From 6724a71320f5a0aaac2a3938b7de61a12062566d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Thu, 7 Nov 2024 17:03:01 +0100 Subject: [PATCH] refactor: better infrastructure for transports as Caddy modules --- caddy/bolt.go | 8 ++++---- caddy/local.go | 10 +++++----- caddy/mercure.go | 19 +++++++++---------- caddy/transport.go | 20 +++++++++++++++----- 4 files changed, 33 insertions(+), 24 deletions(-) diff --git a/caddy/bolt.go b/caddy/bolt.go index 81ddd136..50fd235d 100644 --- a/caddy/bolt.go +++ b/caddy/bolt.go @@ -46,26 +46,26 @@ func (b *Bolt) Provision(ctx caddy.Context) error { } b.transportKey = key.String() - destructor, _, err := transport.LoadOrNew(b.transportKey, func() (caddy.Destructor, error) { + destructor, _, err := TransportUsagePool.LoadOrNew(b.transportKey, func() (caddy.Destructor, error) { t, err := mercure.NewBoltTransport(ctx.Logger(), b.Path, b.BucketName, b.Size, b.CleanupFrequency) if err != nil { return nil, err } - return transportDestructor[*mercure.BoltTransport]{transport: t}, nil + return TransportDestructor[*mercure.BoltTransport]{Transport: t}, nil }) if err != nil { return err } - b.transport = destructor.(transportDestructor[*mercure.BoltTransport]).transport + b.transport = destructor.(TransportDestructor[*mercure.BoltTransport]).Transport return nil } //nolint:wrapcheck func (b *Bolt) Cleanup() error { - _, err := transport.Delete(b.transportKey) + _, err := TransportUsagePool.Delete(b.transportKey) return err } diff --git a/caddy/local.go b/caddy/local.go index 23874b64..56de3ac5 100644 --- a/caddy/local.go +++ b/caddy/local.go @@ -30,20 +30,20 @@ func (l *Local) GetTransport() mercure.Transport { //nolint:ireturn return l.transport } -// Provision provisions b's configuration. +// Provision provisions l's configuration. func (l *Local) Provision(_ caddy.Context) error { - destructor, _, _ := transport.LoadOrNew(localTransportKey, func() (caddy.Destructor, error) { - return transportDestructor[*mercure.LocalTransport]{transport: mercure.NewLocalTransport()}, nil + destructor, _, _ := TransportUsagePool.LoadOrNew(localTransportKey, func() (caddy.Destructor, error) { + return TransportDestructor[*mercure.LocalTransport]{Transport: mercure.NewLocalTransport()}, nil }) - l.transport = destructor.(transportDestructor[*mercure.LocalTransport]).transport + l.transport = destructor.(TransportDestructor[*mercure.LocalTransport]).Transport return nil } //nolint:wrapcheck func (l *Local) Cleanup() error { - _, err := transport.Delete(localTransportKey) + _, err := TransportUsagePool.Delete(localTransportKey) return err } diff --git a/caddy/mercure.go b/caddy/mercure.go index 864fe995..bf8c774e 100644 --- a/caddy/mercure.go +++ b/caddy/mercure.go @@ -178,13 +178,13 @@ func createTransportLegacy(m *Mercure) (mercure.Transport, error) { return nil, err } - return &transportDestructor[mercure.Transport]{transport}, nil + return &TransportDestructor[mercure.Transport]{transport}, nil }) if err != nil { return nil, err } - return destructor.(*transportDestructor[mercure.Transport]).transport, nil + return destructor.(*TransportDestructor[mercure.Transport]).Transport, nil } //nolint:wrapcheck @@ -203,6 +203,9 @@ func (m *Mercure) Provision(ctx caddy.Context) error { //nolint:funlen,gocognit return err } + ctx = ctx.WithValue(SubscriptionsKey, m.Subscriptions) + ctx = ctx.WithValue(WriteTimeoutKey, m.WriteTimeout) + m.logger = ctx.Logger() var transport mercure.Transport @@ -411,21 +414,17 @@ func (m *Mercure) UnmarshalCaddyfile(d *caddyfile.Dispenser) error { //nolint:fu } case "publish_origins": - ra := d.RemainingArgs() - if len(ra) == 0 { + m.PublishOrigins = d.RemainingArgs() + if len(m.PublishOrigins) == 0 { return d.ArgErr() } - m.PublishOrigins = ra - case "cors_origins": - ra := d.RemainingArgs() - if len(ra) == 0 { + m.CORSOrigins = d.RemainingArgs() + if len(m.CORSOrigins) == 0 { return d.ArgErr() } - m.CORSOrigins = ra - case "transport": if !d.NextArg() { return d.ArgErr() diff --git a/caddy/transport.go b/caddy/transport.go index 8a33559f..bf8f139f 100644 --- a/caddy/transport.go +++ b/caddy/transport.go @@ -5,16 +5,26 @@ import ( "github.com/dunglas/mercure" ) -var transport = caddy.NewUsagePool() //nolint:gochecknoglobals +var TransportUsagePool = caddy.NewUsagePool() //nolint:gochecknoglobals type Transport interface { GetTransport() mercure.Transport } -type transportDestructor[T mercure.Transport] struct { - transport T +type TransportDestructor[T mercure.Transport] struct { + Transport T } -func (d transportDestructor[T]) Destruct() error { - return d.transport.Close() //nolint:wrapcheck +func (d TransportDestructor[T]) Destruct() error { + return d.Transport.Close() //nolint:wrapcheck } + +type ( + subscriptionsKeyType struct{} + writeTimeoutKeyType struct{} +) + +var ( + SubscriptionsKey = subscriptionsKeyType{} //nolint:gochecknoglobals + WriteTimeoutKey = writeTimeoutKeyType{} //nolint:gochecknoglobals +)