Skip to content

Commit 335f259

Browse files
committed
chore: simplify, modernize (Go 1.26), update deps
- Fix mutex left locked when CreateListener fails in Serve (deadlock on Stop) - Use ShutdownWithContext(ctx) in Stop so the shutdown respects caller deadline - Call Config.Valid() in Init to surface misconfiguration early - Use := for inner err in Serve goroutine to avoid closing over the outer var - Merge endCh+errCh into a single chan error in Stop - Remove 7 explicit zero-value fields from fiber.Config (noise) - Use range-over-value (Go 1.22) in Serve and Config.Valid
1 parent dfd6df5 commit 335f259

2 files changed

Lines changed: 30 additions & 44 deletions

File tree

config.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,21 @@ func (c *Config) Valid() error {
4141
return errors.E(op, errors.Str("no configuration to serve"))
4242
}
4343

44-
for i := range c.Configuration {
45-
if c.Configuration[i].Prefix == "" {
44+
for _, cfg := range c.Configuration {
45+
if cfg.Prefix == "" {
4646
return errors.E(op, errors.Str("empty prefix"))
4747
}
4848

49-
if c.Configuration[i].Prefix[0] != '/' {
49+
if cfg.Prefix[0] != '/' {
5050
return errors.E(op, errors.Str("prefix must begin with a forward slash"))
5151
}
5252

53-
if c.Configuration[i].Root == "" {
54-
c.Configuration[i].Root = "."
53+
if cfg.Root == "" {
54+
cfg.Root = "."
5555
}
5656

57-
if c.Configuration[i].CacheDuration == 0 {
58-
c.Configuration[i].CacheDuration = 10
57+
if cfg.CacheDuration == 0 {
58+
cfg.CacheDuration = 10
5959
}
6060
}
6161

plugin.go

Lines changed: 23 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ func (p *Plugin) Init(cfg Configurer, log Logger) error {
4545
return errors.E(op, err)
4646
}
4747

48+
if err = p.config.Valid(); err != nil {
49+
return errors.E(op, err)
50+
}
51+
4852
p.log = log.NamedLogger(pluginName)
4953

5054
return nil
@@ -55,20 +59,13 @@ func (p *Plugin) Serve() chan error {
5559

5660
p.Lock()
5761
p.app = fiber.New(fiber.Config{
58-
ReadBufferSize: 1 * 1024 * 1024,
59-
WriteBufferSize: 1 * 1024 * 1024,
60-
Prefork: false,
61-
BodyLimit: 10 * 1024 * 1024,
62-
ReadTimeout: time.Second * 10,
63-
WriteTimeout: time.Second * 10,
64-
DisableKeepalive: false,
65-
DisableDefaultDate: false,
66-
DisableDefaultContentType: false,
67-
DisableHeaderNormalizing: false,
68-
DisableStartupMessage: true,
69-
StreamRequestBody: p.config.StreamRequestBody,
70-
DisablePreParseMultipartForm: false,
71-
ReduceMemoryUsage: false,
62+
ReadBufferSize: 1 * 1024 * 1024,
63+
WriteBufferSize: 1 * 1024 * 1024,
64+
BodyLimit: 10 * 1024 * 1024,
65+
ReadTimeout: time.Second * 10,
66+
WriteTimeout: time.Second * 10,
67+
DisableStartupMessage: true,
68+
StreamRequestBody: p.config.StreamRequestBody,
7269
})
7370

7471
if p.config.CalculateEtag {
@@ -77,59 +74,48 @@ func (p *Plugin) Serve() chan error {
7774
}))
7875
}
7976

80-
for i := range p.config.Configuration {
81-
p.app.Static(p.config.Configuration[i].Prefix, p.config.Configuration[i].Root, fiber.Static{
82-
Compress: p.config.Configuration[i].Compress,
83-
ByteRange: p.config.Configuration[i].BytesRange,
77+
for _, cfg := range p.config.Configuration {
78+
p.app.Static(cfg.Prefix, cfg.Root, fiber.Static{
79+
Compress: cfg.Compress,
80+
ByteRange: cfg.BytesRange,
8481
Browse: false,
85-
CacheDuration: time.Second * time.Duration(p.config.Configuration[i].CacheDuration),
86-
MaxAge: p.config.Configuration[i].MaxAge,
82+
CacheDuration: time.Second * time.Duration(cfg.CacheDuration),
83+
MaxAge: cfg.MaxAge,
8784
})
8885
}
8986

9087
ln, err := tcplisten.CreateListener(p.config.Address)
9188
if err != nil {
89+
p.Unlock()
9290
errCh <- err
9391
return errCh
9492
}
9593

9694
go func() {
9795
p.Unlock()
9896
p.log.Info("file server started", "address", p.config.Address)
99-
err = p.app.Listener(ln)
100-
if err != nil {
97+
if err := p.app.Listener(ln); err != nil {
10198
errCh <- err
102-
return
10399
}
104100
}()
105101

106102
return errCh
107103
}
108104

109105
func (p *Plugin) Stop(ctx context.Context) error {
110-
endCh := make(chan struct{}, 1)
111-
errCh := make(chan error, 1)
106+
doneCh := make(chan error, 1)
112107

113108
go func() {
114109
p.Lock()
115110
defer p.Unlock()
116-
117-
err := p.app.Shutdown()
118-
if err != nil {
119-
errCh <- err
120-
return
121-
}
122-
123-
endCh <- struct{}{}
111+
doneCh <- p.app.ShutdownWithContext(ctx)
124112
}()
125113

126114
select {
127115
case <-ctx.Done():
128116
return ctx.Err()
129-
case e := <-errCh:
130-
return e
131-
case <-endCh:
132-
return nil
117+
case err := <-doneCh:
118+
return err
133119
}
134120
}
135121

0 commit comments

Comments
 (0)