|
60 | 60 | isRunning bool |
61 | 61 | onServerShutdown []func() |
62 | 62 |
|
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() |
66 | 66 | globalLogger = slog.Default() |
67 | 67 |
|
68 | 68 | metrics Metrics = nullMetrics{} |
@@ -235,18 +235,12 @@ func Init(options ...Option) error { |
235 | 235 |
|
236 | 236 | globalMu.Lock() |
237 | 237 |
|
238 | | - if opt.ctx == nil { |
239 | | - globalCtx = context.Background() |
240 | | - } else { |
| 238 | + if opt.ctx != nil { |
241 | 239 | globalCtx = opt.ctx |
242 | 240 | opt.ctx = nil |
243 | 241 | } |
244 | 242 |
|
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 { |
250 | 244 | globalLogger = opt.logger |
251 | 245 | opt.logger = nil |
252 | 246 | } |
@@ -352,11 +346,7 @@ func Shutdown() { |
352 | 346 | globalLogger.LogAttrs(globalCtx, slog.LevelDebug, "FrankenPHP shut down") |
353 | 347 | } |
354 | 348 |
|
355 | | - globalMu.Lock() |
356 | | - globalCtx = context.TODO() |
357 | | - globalLogger = slog.Default() |
358 | | - workers = nil |
359 | | - globalMu.Unlock() |
| 349 | + resetGlobals() |
360 | 350 | } |
361 | 351 |
|
362 | 352 | // ServeHTTP executes a PHP script according to the given context. |
@@ -722,3 +712,11 @@ func timeoutChan(timeout time.Duration) <-chan time.Time { |
722 | 712 |
|
723 | 713 | return time.After(timeout) |
724 | 714 | } |
| 715 | + |
| 716 | +func resetGlobals() { |
| 717 | + globalMu.Lock() |
| 718 | + globalCtx = context.Background() |
| 719 | + globalLogger = slog.Default() |
| 720 | + workers = nil |
| 721 | + globalMu.Unlock() |
| 722 | +} |
0 commit comments