Skip to content

Commit 0a6bd9f

Browse files
authored
refactor: better infrastructure for transports as Caddy modules (#975)
1 parent 05e5ef2 commit 0a6bd9f

File tree

4 files changed

+33
-24
lines changed

4 files changed

+33
-24
lines changed

caddy/bolt.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -46,26 +46,26 @@ func (b *Bolt) Provision(ctx caddy.Context) error {
4646
}
4747
b.transportKey = key.String()
4848

49-
destructor, _, err := transport.LoadOrNew(b.transportKey, func() (caddy.Destructor, error) {
49+
destructor, _, err := TransportUsagePool.LoadOrNew(b.transportKey, func() (caddy.Destructor, error) {
5050
t, err := mercure.NewBoltTransport(ctx.Logger(), b.Path, b.BucketName, b.Size, b.CleanupFrequency)
5151
if err != nil {
5252
return nil, err
5353
}
5454

55-
return transportDestructor[*mercure.BoltTransport]{transport: t}, nil
55+
return TransportDestructor[*mercure.BoltTransport]{Transport: t}, nil
5656
})
5757
if err != nil {
5858
return err
5959
}
6060

61-
b.transport = destructor.(transportDestructor[*mercure.BoltTransport]).transport
61+
b.transport = destructor.(TransportDestructor[*mercure.BoltTransport]).Transport
6262

6363
return nil
6464
}
6565

6666
//nolint:wrapcheck
6767
func (b *Bolt) Cleanup() error {
68-
_, err := transport.Delete(b.transportKey)
68+
_, err := TransportUsagePool.Delete(b.transportKey)
6969

7070
return err
7171
}

caddy/local.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,20 @@ func (l *Local) GetTransport() mercure.Transport { //nolint:ireturn
3030
return l.transport
3131
}
3232

33-
// Provision provisions b's configuration.
33+
// Provision provisions l's configuration.
3434
func (l *Local) Provision(_ caddy.Context) error {
35-
destructor, _, _ := transport.LoadOrNew(localTransportKey, func() (caddy.Destructor, error) {
36-
return transportDestructor[*mercure.LocalTransport]{transport: mercure.NewLocalTransport()}, nil
35+
destructor, _, _ := TransportUsagePool.LoadOrNew(localTransportKey, func() (caddy.Destructor, error) {
36+
return TransportDestructor[*mercure.LocalTransport]{Transport: mercure.NewLocalTransport()}, nil
3737
})
3838

39-
l.transport = destructor.(transportDestructor[*mercure.LocalTransport]).transport
39+
l.transport = destructor.(TransportDestructor[*mercure.LocalTransport]).Transport
4040

4141
return nil
4242
}
4343

4444
//nolint:wrapcheck
4545
func (l *Local) Cleanup() error {
46-
_, err := transport.Delete(localTransportKey)
46+
_, err := TransportUsagePool.Delete(localTransportKey)
4747

4848
return err
4949
}

caddy/mercure.go

+9-10
Original file line numberDiff line numberDiff line change
@@ -178,13 +178,13 @@ func createTransportLegacy(m *Mercure) (mercure.Transport, error) {
178178
return nil, err
179179
}
180180

181-
return &transportDestructor[mercure.Transport]{transport}, nil
181+
return &TransportDestructor[mercure.Transport]{transport}, nil
182182
})
183183
if err != nil {
184184
return nil, err
185185
}
186186

187-
return destructor.(*transportDestructor[mercure.Transport]).transport, nil
187+
return destructor.(*TransportDestructor[mercure.Transport]).Transport, nil
188188
}
189189

190190
//nolint:wrapcheck
@@ -203,6 +203,9 @@ func (m *Mercure) Provision(ctx caddy.Context) error { //nolint:funlen,gocognit
203203
return err
204204
}
205205

206+
ctx = ctx.WithValue(SubscriptionsContextKey, m.Subscriptions)
207+
ctx = ctx.WithValue(WriteTimeoutContextKey, m.WriteTimeout)
208+
206209
m.logger = ctx.Logger()
207210

208211
var transport mercure.Transport
@@ -411,21 +414,17 @@ func (m *Mercure) UnmarshalCaddyfile(d *caddyfile.Dispenser) error { //nolint:fu
411414
}
412415

413416
case "publish_origins":
414-
ra := d.RemainingArgs()
415-
if len(ra) == 0 {
417+
m.PublishOrigins = d.RemainingArgs()
418+
if len(m.PublishOrigins) == 0 {
416419
return d.ArgErr()
417420
}
418421

419-
m.PublishOrigins = ra
420-
421422
case "cors_origins":
422-
ra := d.RemainingArgs()
423-
if len(ra) == 0 {
423+
m.CORSOrigins = d.RemainingArgs()
424+
if len(m.CORSOrigins) == 0 {
424425
return d.ArgErr()
425426
}
426427

427-
m.CORSOrigins = ra
428-
429428
case "transport":
430429
if !d.NextArg() {
431430
return d.ArgErr()

caddy/transport.go

+15-5
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,26 @@ import (
55
"github.com/dunglas/mercure"
66
)
77

8-
var transport = caddy.NewUsagePool() //nolint:gochecknoglobals
8+
var TransportUsagePool = caddy.NewUsagePool() //nolint:gochecknoglobals
99

1010
type Transport interface {
1111
GetTransport() mercure.Transport
1212
}
1313

14-
type transportDestructor[T mercure.Transport] struct {
15-
transport T
14+
type TransportDestructor[T mercure.Transport] struct {
15+
Transport T
1616
}
1717

18-
func (d transportDestructor[T]) Destruct() error {
19-
return d.transport.Close() //nolint:wrapcheck
18+
func (d TransportDestructor[T]) Destruct() error {
19+
return d.Transport.Close() //nolint:wrapcheck
2020
}
21+
22+
type (
23+
subscriptionsKeyType struct{}
24+
writeTimeoutKeyType struct{}
25+
)
26+
27+
var (
28+
SubscriptionsContextKey = subscriptionsKeyType{} //nolint:gochecknoglobals
29+
WriteTimeoutContextKey = writeTimeoutKeyType{} //nolint:gochecknoglobals
30+
)

0 commit comments

Comments
 (0)