Skip to content

Commit

Permalink
refactor: better infrastructure for transports as Caddy modules
Browse files Browse the repository at this point in the history
  • Loading branch information
dunglas committed Nov 9, 2024
1 parent fbd473b commit 6724a71
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 24 deletions.
8 changes: 4 additions & 4 deletions caddy/bolt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
10 changes: 5 additions & 5 deletions caddy/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
19 changes: 9 additions & 10 deletions caddy/mercure.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand Down
20 changes: 15 additions & 5 deletions caddy/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

0 comments on commit 6724a71

Please sign in to comment.