Skip to content

Commit 8341cc9

Browse files
dunglasCopilot
andauthored
refactor: rely on context.Context for log/slog and others (#1969)
* refactor: rely on context.Context for log/slog and others * optimize * refactor * Apply suggestion from @Copilot Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * fix watcher-skip * better globals handling * fix --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 40cb42a commit 8341cc9

23 files changed

Lines changed: 425 additions & 183 deletions

caddy/app.go

Lines changed: 10 additions & 2 deletions
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),
@@ -159,7 +163,11 @@ func (f *FrankenPHPApp) Start() error {
159163
}
160164

161165
func (f *FrankenPHPApp) Stop() error {
162-
f.logger.Info("FrankenPHP stopped 🐘")
166+
ctx := caddy.ActiveContext()
167+
168+
if f.logger.Enabled(caddy.ActiveContext(), slog.LevelInfo) {
169+
f.logger.LogAttrs(ctx, slog.LevelInfo, "FrankenPHP stopped 🐘")
170+
}
163171

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

cgi.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ func addPreparedEnvToServer(fc *frankenPHPContext, trackVarsArray *C.zval) {
212212
//export go_register_variables
213213
func go_register_variables(threadIndex C.uintptr_t, trackVarsArray *C.zval) {
214214
thread := phpThreads[threadIndex]
215-
fc := thread.getRequestContext()
215+
fc := thread.frankenPHPContext()
216216

217217
if fc.request != nil {
218218
addKnownVariablesToServer(thread, fc, trackVarsArray)
@@ -279,7 +279,7 @@ func splitPos(path string, splitPath []string) int {
279279
//export go_update_request_info
280280
func go_update_request_info(threadIndex C.uintptr_t, info *C.sapi_request_info) C.bool {
281281
thread := phpThreads[threadIndex]
282-
fc := thread.getRequestContext()
282+
fc := thread.frankenPHPContext()
283283
request := fc.request
284284

285285
if request == nil {

context.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ type frankenPHPContext struct {
3838
startedAt time.Time
3939
}
4040

41+
type contextHolder struct {
42+
ctx context.Context
43+
frankenPHPContext *frankenPHPContext
44+
}
45+
4146
// fromContext extracts the frankenPHPContext from a context.
4247
func fromContext(ctx context.Context) (fctx *frankenPHPContext, ok bool) {
4348
fctx, ok = ctx.Value(contextKey).(*frankenPHPContext)
@@ -63,7 +68,7 @@ func NewRequestWithContext(r *http.Request, opts ...RequestOption) (*http.Reques
6368
}
6469

6570
if fc.logger == nil {
66-
fc.logger = logger
71+
fc.logger = globalLogger
6772
}
6873

6974
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) {

0 commit comments

Comments
 (0)