Skip to content

Commit 7615925

Browse files
committed
better globals handling
1 parent 53da11e commit 7615925

3 files changed

Lines changed: 21 additions & 28 deletions

File tree

caddy/app.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package caddy
22

33
import (
4+
"context"
45
"errors"
56
"fmt"
67
"log/slog"
@@ -56,6 +57,7 @@ type FrankenPHPApp struct {
5657
MaxWaitTime time.Duration `json:"max_wait_time,omitempty"`
5758

5859
metrics frankenphp.Metrics
60+
ctx context.Context
5961
logger *slog.Logger
6062
}
6163

@@ -71,6 +73,7 @@ func (f FrankenPHPApp) CaddyModule() caddy.ModuleInfo {
7173

7274
// Provision sets up the module.
7375
func (f *FrankenPHPApp) Provision(ctx caddy.Context) error {
76+
f.ctx = ctx
7477
f.logger = ctx.Slogger()
7578

7679
if httpApp, err := ctx.AppIfConfigured("http"); err == nil {
@@ -128,9 +131,10 @@ func (f *FrankenPHPApp) Start() error {
128131
repl := caddy.NewReplacer()
129132

130133
opts := []frankenphp.Option{
134+
frankenphp.WithContext(f.ctx),
135+
frankenphp.WithLogger(f.logger),
131136
frankenphp.WithNumThreads(f.NumThreads),
132137
frankenphp.WithMaxThreads(f.MaxThreads),
133-
frankenphp.WithLogger(f.logger),
134138
frankenphp.WithMetrics(f.metrics),
135139
frankenphp.WithPhpIni(f.PhpIni),
136140
frankenphp.WithMaxWaitTime(f.MaxWaitTime),

frankenphp.go

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ var (
6060
isRunning bool
6161
onServerShutdown []func()
6262

63-
globalMu sync.RWMutex
64-
// Set default values because to make Shutdown() idempotent
65-
globalCtx = context.TODO()
63+
// Set default values to make Shutdown() idempotent
64+
globalMu sync.Mutex
65+
globalCtx = context.Background()
6666
globalLogger = slog.Default()
6767

6868
metrics Metrics = nullMetrics{}
@@ -235,18 +235,12 @@ func Init(options ...Option) error {
235235

236236
globalMu.Lock()
237237

238-
if opt.ctx == nil {
239-
globalCtx = context.Background()
240-
} else {
238+
if opt.ctx != nil {
241239
globalCtx = opt.ctx
242240
opt.ctx = nil
243241
}
244242

245-
if opt.logger == nil {
246-
// set a default globalLogger
247-
// to disable logging, set the globalLogger to slog.New(slog.DiscardHandler)
248-
opt.logger = slog.Default()
249-
} else {
243+
if opt.logger != nil {
250244
globalLogger = opt.logger
251245
opt.logger = nil
252246
}
@@ -352,11 +346,7 @@ func Shutdown() {
352346
globalLogger.LogAttrs(globalCtx, slog.LevelDebug, "FrankenPHP shut down")
353347
}
354348

355-
globalMu.Lock()
356-
globalCtx = context.TODO()
357-
globalLogger = slog.Default()
358-
workers = nil
359-
globalMu.Unlock()
349+
resetGlobals()
360350
}
361351

362352
// ServeHTTP executes a PHP script according to the given context.
@@ -722,3 +712,11 @@ func timeoutChan(timeout time.Duration) <-chan time.Time {
722712

723713
return time.After(timeout)
724714
}
715+
716+
func resetGlobals() {
717+
globalMu.Lock()
718+
globalCtx = context.Background()
719+
globalLogger = slog.Default()
720+
workers = nil
721+
globalMu.Unlock()
722+
}

phpmainthread_test.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,9 @@ var testDataPath, _ = filepath.Abs("./testdata")
2121
func setupGlobals(t *testing.T) {
2222
t.Helper()
2323

24-
t.Cleanup(func() {
25-
globalMu.Lock()
26-
globalCtx = nil
27-
globalLogger = nil
28-
workers = nil
29-
globalMu.Unlock()
30-
})
24+
t.Cleanup(Shutdown)
3125

32-
globalMu.Lock()
33-
globalCtx = t.Context()
34-
globalLogger = slog.Default()
35-
globalMu.Unlock()
26+
resetGlobals()
3627
}
3728

3829
func TestStartAndStopTheMainThreadWithOneInactiveThread(t *testing.T) {

0 commit comments

Comments
 (0)