Skip to content

Commit 668597e

Browse files
committed
refactor
1 parent 3505793 commit 668597e

18 files changed

Lines changed: 239 additions & 97 deletions

caddy/app.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,11 @@ func (f *FrankenPHPApp) Start() error {
159159
}
160160

161161
func (f *FrankenPHPApp) Stop() error {
162-
f.logger.Info("FrankenPHP stopped 🐘")
162+
ctx := caddy.ActiveContext()
163+
164+
if f.logger.Enabled(caddy.ActiveContext(), slog.LevelInfo) {
165+
f.logger.LogAttrs(ctx, slog.LevelInfo, "FrankenPHP stopped 🐘")
166+
}
163167

164168
// attempt a graceful shutdown if caddy is exiting
165169
// note: Exiting() is currently marked as 'experimental'

context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func NewRequestWithContext(r *http.Request, opts ...RequestOption) (*http.Reques
6868
}
6969

7070
if fc.logger == nil {
71-
fc.logger = logger
71+
fc.logger = globalLogger
7272
}
7373

7474
if fc.documentRoot == "" {

frankenphp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,7 @@ static void frankenphp_register_variables(zval *track_vars_array) {
802802
}
803803

804804
static void frankenphp_log_message(const char *message, int syslog_type_int) {
805-
go_log((char *)message, syslog_type_int);
805+
go_log(thread_index, (char *)message, syslog_type_int);
806806
}
807807

808808
static char *frankenphp_getenv(const char *name, size_t name_len) {

frankenphp.go

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

63-
loggerMu sync.RWMutex
64-
logger *slog.Logger
63+
globalMu sync.RWMutex
64+
// Set default values because to make Shutdown() idempotent
65+
globalCtx = context.TODO()
66+
globalLogger = slog.Default()
6567

6668
metrics Metrics = nullMetrics{}
6769

@@ -231,15 +233,25 @@ func Init(options ...Option) error {
231233
}
232234
}
233235

236+
globalMu.Lock()
237+
238+
if opt.ctx == nil {
239+
globalCtx = context.Background()
240+
} else {
241+
globalCtx = opt.ctx
242+
opt.ctx = nil
243+
}
244+
234245
if opt.logger == nil {
235-
// set a default logger
236-
// to disable logging, set the logger to slog.New(slog.DiscardHandler)
246+
// set a default globalLogger
247+
// to disable logging, set the globalLogger to slog.New(slog.DiscardHandler)
237248
opt.logger = slog.Default()
249+
} else {
250+
globalLogger = opt.logger
251+
opt.logger = nil
238252
}
239253

240-
loggerMu.Lock()
241-
logger = opt.logger
242-
loggerMu.Unlock()
254+
globalMu.Unlock()
243255

244256
if opt.metrics != nil {
245257
metrics = opt.metrics
@@ -262,11 +274,16 @@ func Init(options ...Option) error {
262274

263275
if config.ZTS {
264276
if !config.ZendMaxExecutionTimers && runtime.GOOS == "linux" {
265-
logger.Warn(`Zend Max Execution Timers are not enabled, timeouts (e.g. "max_execution_time") are disabled, recompile PHP with the "--enable-zend-max-execution-timers" configuration option to fix this issue`)
277+
if globalLogger.Enabled(globalCtx, slog.LevelWarn) {
278+
globalLogger.LogAttrs(globalCtx, slog.LevelWarn, `Zend Max Execution Timers are not enabled, timeouts (e.g. "max_execution_time") are disabled, recompile PHP with the "--enable-zend-max-execution-timers" configuration option to fix this issue`)
279+
}
266280
}
267281
} else {
268282
opt.numThreads = 1
269-
logger.Warn(`ZTS is not enabled, only 1 thread will be available, recompile PHP using the "--enable-zts" configuration option or performance will be degraded`)
283+
284+
if globalLogger.Enabled(globalCtx, slog.LevelWarn) {
285+
globalLogger.LogAttrs(globalCtx, slog.LevelWarn, `ZTS is not enabled, only 1 thread will be available, recompile PHP using the "--enable-zts" configuration option or performance will be degraded`)
286+
}
270287
}
271288

272289
mainThread, err := initPHPThreads(opt.numThreads, opt.maxThreads, opt.phpIni)
@@ -286,10 +303,12 @@ func Init(options ...Option) error {
286303

287304
initAutoScaling(mainThread)
288305

289-
ctx := context.Background()
290-
logger.LogAttrs(ctx, slog.LevelInfo, "FrankenPHP started 🐘", slog.String("php_version", Version().Version), slog.Int("num_threads", mainThread.numThreads), slog.Int("max_threads", mainThread.maxThreads))
291-
if EmbeddedAppPath != "" {
292-
logger.LogAttrs(ctx, slog.LevelInfo, "embedded PHP app 📦", slog.String("path", EmbeddedAppPath))
306+
if globalLogger.Enabled(globalCtx, slog.LevelInfo) {
307+
globalLogger.LogAttrs(globalCtx, slog.LevelInfo, "FrankenPHP started 🐘", slog.String("php_version", Version().Version), slog.Int("num_threads", mainThread.numThreads), slog.Int("max_threads", mainThread.maxThreads))
308+
309+
if EmbeddedAppPath != "" {
310+
globalLogger.LogAttrs(globalCtx, slog.LevelInfo, "embedded PHP app 📦", slog.String("path", EmbeddedAppPath))
311+
}
293312
}
294313

295314
// register the startup/shutdown hooks (mainly useful for extensions)
@@ -329,7 +348,15 @@ func Shutdown() {
329348
}
330349

331350
isRunning = false
332-
logger.Debug("FrankenPHP shut down")
351+
if globalLogger.Enabled(globalCtx, slog.LevelDebug) {
352+
globalLogger.LogAttrs(globalCtx, slog.LevelDebug, "FrankenPHP shut down")
353+
}
354+
355+
globalMu.Lock()
356+
globalCtx = context.TODO()
357+
globalLogger = slog.Default()
358+
workers = nil
359+
globalMu.Unlock()
333360
}
334361

335362
// ServeHTTP executes a PHP script according to the given context.
@@ -420,8 +447,8 @@ func go_apache_request_headers(threadIndex C.uintptr_t) (*C.go_string, C.size_t)
420447
if fc.responseWriter == nil {
421448
// worker mode, not handling a request
422449

423-
if logger.Enabled(ctx, slog.LevelDebug) {
424-
logger.LogAttrs(ctx, slog.LevelDebug, "apache_request_headers() called in non-HTTP context", slog.String("worker", fc.worker.name))
450+
if globalLogger.Enabled(ctx, slog.LevelDebug) {
451+
globalLogger.LogAttrs(ctx, slog.LevelDebug, "apache_request_headers() called in non-HTTP context", slog.String("worker", fc.worker.name))
425452
}
426453

427454
return nil, 0
@@ -450,10 +477,13 @@ func go_apache_request_headers(threadIndex C.uintptr_t) (*C.go_string, C.size_t)
450477
return sd, C.size_t(len(fc.request.Header))
451478
}
452479

453-
func addHeader(fc *frankenPHPContext, cString *C.char, length C.int) {
480+
func addHeader(ctx context.Context, fc *frankenPHPContext, cString *C.char, length C.int) {
454481
key, val := splitRawHeader(cString, int(length))
455482
if key == "" {
456-
fc.logger.LogAttrs(context.Background(), slog.LevelDebug, "invalid header", slog.String("header", C.GoStringN(cString, length)))
483+
if fc.logger.Enabled(ctx, slog.LevelDebug) {
484+
fc.logger.LogAttrs(ctx, slog.LevelDebug, "invalid header", slog.String("header", C.GoStringN(cString, length)))
485+
}
486+
457487
return
458488
}
459489
fc.responseWriter.Header().Add(key, val)
@@ -511,7 +541,7 @@ func go_write_headers(threadIndex C.uintptr_t, status C.int, headers *C.zend_lli
511541
for current != nil {
512542
h := (*C.sapi_header_struct)(unsafe.Pointer(&(current.data)))
513543

514-
addHeader(fc, h.header, C.int(h.header_len))
544+
addHeader(thread.context(), fc, h.header, C.int(h.header_len))
515545
current = current.next
516546
}
517547

@@ -522,8 +552,8 @@ func go_write_headers(threadIndex C.uintptr_t, status C.int, headers *C.zend_lli
522552
if goStatus < 100 || goStatus > 999 {
523553
ctx := thread.context()
524554

525-
if logger.Enabled(ctx, slog.LevelWarn) {
526-
logger.LogAttrs(ctx, slog.LevelWarn, "Invalid response status code", slog.Int("status_code", goStatus))
555+
if globalLogger.Enabled(ctx, slog.LevelWarn) {
556+
globalLogger.LogAttrs(ctx, slog.LevelWarn, "Invalid response status code", slog.Int("status_code", goStatus))
527557
}
528558

529559
goStatus = 500
@@ -561,8 +591,8 @@ func go_sapi_flush(threadIndex C.uintptr_t) bool {
561591
if err := http.NewResponseController(fc.responseWriter).Flush(); err != nil {
562592
ctx := thread.context()
563593

564-
if logger.Enabled(ctx, slog.LevelWarn) {
565-
logger.LogAttrs(ctx, slog.LevelWarn, "the current responseWriter is not a flusher, if you are not using a custom build, please report this issue", slog.Any("error", err))
594+
if globalLogger.Enabled(ctx, slog.LevelWarn) {
595+
globalLogger.LogAttrs(ctx, slog.LevelWarn, "the current responseWriter is not a flusher, if you are not using a custom build, please report this issue", slog.Any("error", err))
566596
}
567597
}
568598

@@ -608,7 +638,8 @@ func go_read_cookies(threadIndex C.uintptr_t) *C.char {
608638
}
609639

610640
//export go_log
611-
func go_log(message *C.char, level C.int) {
641+
func go_log(threadIndex C.uintptr_t, message *C.char, level C.int) {
642+
ctx := phpThreads[threadIndex].context()
612643
m := C.GoString(message)
613644

614645
var le syslogLevel
@@ -620,15 +651,23 @@ func go_log(message *C.char, level C.int) {
620651

621652
switch le {
622653
case syslogLevelEmerg, syslogLevelAlert, syslogLevelCrit, syslogLevelErr:
623-
logger.LogAttrs(context.Background(), slog.LevelError, m, slog.String("syslog_level", syslogLevel(level).String()))
654+
if globalLogger.Enabled(ctx, slog.LevelError) {
655+
globalLogger.LogAttrs(ctx, slog.LevelError, m, slog.String("syslog_level", syslogLevel(level).String()))
656+
}
624657

625658
case syslogLevelWarn:
626-
logger.LogAttrs(context.Background(), slog.LevelWarn, m, slog.String("syslog_level", syslogLevel(level).String()))
659+
if globalLogger.Enabled(ctx, slog.LevelWarn) {
660+
globalLogger.LogAttrs(ctx, slog.LevelWarn, m, slog.String("syslog_level", syslogLevel(level).String()))
661+
}
627662
case syslogLevelDebug:
628-
logger.LogAttrs(context.Background(), slog.LevelDebug, m, slog.String("syslog_level", syslogLevel(level).String()))
663+
if globalLogger.Enabled(ctx, slog.LevelDebug) {
664+
globalLogger.LogAttrs(ctx, slog.LevelDebug, m, slog.String("syslog_level", syslogLevel(level).String()))
665+
}
629666

630667
default:
631-
logger.LogAttrs(context.Background(), slog.LevelInfo, m, slog.String("syslog_level", syslogLevel(level).String()))
668+
if globalLogger.Enabled(ctx, slog.LevelInfo) {
669+
globalLogger.LogAttrs(ctx, slog.LevelInfo, m, slog.String("syslog_level", syslogLevel(level).String()))
670+
}
632671
}
633672
}
634673

internal/testserver/main.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@ import (
1010
)
1111

1212
func main() {
13+
ctx := context.Background()
1314
logger := slog.New(slog.NewTextHandler(os.Stdout, nil))
14-
if err := frankenphp.Init(frankenphp.WithLogger(logger)); err != nil {
15+
16+
17+
if err := frankenphp.Init(frankenphp.WithContext(ctx), frankenphp.WithLogger(logger)); err != nil {
1518
panic(err)
1619
}
1720
defer frankenphp.Shutdown()
@@ -32,6 +35,9 @@ func main() {
3235
port = "8080"
3336
}
3437

35-
logger.LogAttrs(context.Background(), slog.LevelError, "server error", slog.Any("error", http.ListenAndServe(":"+port, nil)))
38+
if logger.Enabled(ctx, slog.LevelError) {
39+
logger.LogAttrs(ctx, slog.LevelError, "server error", slog.Any("error", http.ListenAndServe(":"+port, nil)))
40+
}
41+
3642
os.Exit(1)
3743
}

internal/watcher/watch_pattern.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
package watcher
44

55
import (
6-
"context"
76
"log/slog"
87
"path/filepath"
98
"strings"
@@ -81,9 +80,10 @@ func isValidEventType(eventType int) bool {
8180

8281
// 0:dir,1:file,2:hard_link,3:sym_link,4:watcher,5:other,
8382
func isValidPathType(pathType int, fileName string) bool {
84-
if pathType == 4 {
85-
logger.LogAttrs(context.Background(), slog.LevelDebug, "special edant/watcher event", slog.String("fileName", fileName))
83+
if pathType == 4 && logger.Enabled(ctx, slog.LevelDebug) {
84+
logger.LogAttrs(ctx, slog.LevelDebug, "special edant/watcher event", slog.String("fileName", fileName))
8685
}
86+
8787
return pathType <= 2
8888
}
8989

@@ -163,9 +163,14 @@ func matchPattern(pattern string, fileName string) bool {
163163
if pattern == "" {
164164
return true
165165
}
166+
166167
patternMatches, err := filepath.Match(pattern, fileName)
168+
167169
if err != nil {
168-
logger.LogAttrs(context.Background(), slog.LevelError, "failed to match filename", slog.String("file", fileName), slog.Any("error", err))
170+
if logger.Enabled(ctx, slog.LevelError) {
171+
logger.LogAttrs(ctx, slog.LevelError, "failed to match filename", slog.String("file", fileName), slog.Any("error", err))
172+
}
173+
169174
return false
170175
}
171176

0 commit comments

Comments
 (0)