Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: better infrastructure for transports as Caddy modules #975

Merged
merged 2 commits into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(SubscriptionsContextKey, m.Subscriptions)
ctx = ctx.WithValue(WriteTimeoutContextKey, 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 (
SubscriptionsContextKey = subscriptionsKeyType{} //nolint:gochecknoglobals
WriteTimeoutContextKey = writeTimeoutKeyType{} //nolint:gochecknoglobals
)
Loading