@@ -233,8 +233,8 @@ func Init(options ...Option) error {
233233
234234 if opt .logger == nil {
235235 // set a default logger
236- // to disable logging, set the logger to slog.New(slog.NewTextHandler(io.Discard, nil) )
237- opt .logger = slog .New ( slog . NewTextHandler ( os . Stdout , nil ) )
236+ // to disable logging, set the logger to slog.New(slog.DiscardHandler )
237+ opt .logger = slog .Default ( )
238238 }
239239
240240 loggerMu .Lock ()
@@ -274,7 +274,7 @@ func Init(options ...Option) error {
274274 return err
275275 }
276276
277- regularRequestChan = make (chan * frankenPHPContext , opt .numThreads - workerThreadCount )
277+ regularRequestChan = make (chan context. Context , opt .numThreads - workerThreadCount )
278278 regularThreads = make ([]* phpThread , 0 , opt .numThreads - workerThreadCount )
279279 for i := 0 ; i < opt .numThreads - workerThreadCount ; i ++ {
280280 convertToRegularThread (getInactivePHPThread ())
@@ -343,7 +343,9 @@ func ServeHTTP(responseWriter http.ResponseWriter, request *http.Request) error
343343 return ErrNotRunning
344344 }
345345
346- fc , ok := fromContext (request .Context ())
346+ ctx := request .Context ()
347+ fc , ok := fromContext (ctx )
348+
347349 if ! ok {
348350 return ErrInvalidRequest
349351 }
@@ -356,16 +358,17 @@ func ServeHTTP(responseWriter http.ResponseWriter, request *http.Request) error
356358
357359 // Detect if a worker is available to handle this request
358360 if fc .worker != nil {
359- return fc .worker .handleRequest (fc )
361+ return fc .worker .handleRequest (ctx )
360362 }
361363
362364 // If no worker was available, send the request to non-worker threads
363- return handleRequestWithRegularPHPThreads (fc )
365+ return handleRequestWithRegularPHPThreads (ctx )
364366}
365367
366368//export go_ub_write
367369func go_ub_write (threadIndex C.uintptr_t , cBuf * C.char , length C.int ) (C.size_t , C.bool ) {
368- fc := phpThreads [threadIndex ].getRequestContext ()
370+ ctx := phpThreads [threadIndex ].context ()
371+ fc := ctx .Value (contextKey ).(* frankenPHPContext )
369372
370373 if fc .isDone {
371374 return 0 , C .bool (true )
@@ -381,13 +384,13 @@ func go_ub_write(threadIndex C.uintptr_t, cBuf *C.char, length C.int) (C.size_t,
381384 }
382385
383386 i , e := writer .Write (unsafe .Slice ((* byte )(unsafe .Pointer (cBuf )), length ))
384- if e != nil {
385- fc .logger .LogAttrs (context . Background () , slog .LevelWarn , "write error" , slog .Any ("error" , e ))
387+ if e != nil && fc . logger . Enabled ( ctx , slog . LevelWarn ) {
388+ fc .logger .LogAttrs (ctx , slog .LevelWarn , "write error" , slog .Any ("error" , e ))
386389 }
387390
388- if fc .responseWriter == nil {
391+ if fc .responseWriter == nil && fc . logger . Enabled ( ctx , slog . LevelInfo ) {
389392 // probably starting a worker script, log the output
390- fc .logger .Info ( writer .(* bytes.Buffer ).String ())
393+ fc .logger .LogAttrs ( ctx , slog . LevelInfo , writer .(* bytes.Buffer ).String ())
391394 }
392395
393396 return C .size_t (i ), C .bool (fc .clientHasClosed ())
@@ -396,12 +399,15 @@ func go_ub_write(threadIndex C.uintptr_t, cBuf *C.char, length C.int) (C.size_t,
396399//export go_apache_request_headers
397400func go_apache_request_headers (threadIndex C.uintptr_t ) (* C.go_string , C.size_t ) {
398401 thread := phpThreads [threadIndex ]
399- fc := thread .getRequestContext ()
402+ ctx := thread .context ()
403+ fc := ctx .Value (contextKey ).(* frankenPHPContext )
400404
401405 if fc .responseWriter == nil {
402406 // worker mode, not handling a request
403407
404- logger .LogAttrs (context .Background (), slog .LevelDebug , "apache_request_headers() called in non-HTTP context" , slog .String ("worker" , fc .worker .name ))
408+ if logger .Enabled (ctx , slog .LevelDebug ) {
409+ logger .LogAttrs (ctx , slog .LevelDebug , "apache_request_headers() called in non-HTTP context" , slog .String ("worker" , fc .worker .name ))
410+ }
405411
406412 return nil , 0
407413 }
@@ -471,12 +477,13 @@ func splitRawHeader(rawHeader *C.char, length int) (string, string) {
471477
472478//export go_write_headers
473479func go_write_headers (threadIndex C.uintptr_t , status C.int , headers * C.zend_llist ) C.bool {
474- fc := phpThreads [threadIndex ].getRequestContext ()
475-
476- if fc == nil {
480+ ctx := phpThreads [threadIndex ].context ()
481+ if ctx == nil {
477482 return C .bool (false )
478483 }
479484
485+ fc := ctx .Value (contextKey ).(* frankenPHPContext )
486+
480487 if fc .isDone {
481488 return C .bool (false )
482489 }
@@ -499,13 +506,16 @@ func go_write_headers(threadIndex C.uintptr_t, status C.int, headers *C.zend_lli
499506 // go panics on invalid status code
500507 // https://github.com/golang/go/blob/9b8742f2e79438b9442afa4c0a0139d3937ea33f/src/net/http/server.go#L1162
501508 if goStatus < 100 || goStatus > 999 {
502- logger .Warn (fmt .Sprintf ("Invalid response status code %v" , goStatus ))
509+ if logger .Enabled (ctx , slog .LevelWarn ) {
510+ logger .LogAttrs (ctx , slog .LevelWarn , "Invalid response status code" , slog .Int ("status_code" , goStatus ))
511+ }
512+
503513 goStatus = 500
504514 }
505515
506516 fc .responseWriter .WriteHeader (goStatus )
507517
508- if goStatus >= 100 && goStatus < 200 {
518+ if goStatus < 200 {
509519 // Clear headers, it's not automatically done by ResponseWriter.WriteHeader() for 1xx responses
510520 h := fc .responseWriter .Header ()
511521 for k := range h {
@@ -518,25 +528,31 @@ func go_write_headers(threadIndex C.uintptr_t, status C.int, headers *C.zend_lli
518528
519529//export go_sapi_flush
520530func go_sapi_flush (threadIndex C.uintptr_t ) bool {
521- fc := phpThreads [threadIndex ].getRequestContext ()
522- if fc == nil || fc .responseWriter == nil {
531+ ctx := phpThreads [threadIndex ].context ()
532+ if ctx == nil {
533+ return false
534+ }
535+
536+ fc := ctx .Value (contextKey ).(* frankenPHPContext )
537+
538+ if fc .responseWriter == nil {
523539 return false
524540 }
525541
526542 if fc .clientHasClosed () && ! fc .isDone {
527543 return true
528544 }
529545
530- if err := http .NewResponseController (fc .responseWriter ).Flush (); err != nil {
531- logger .LogAttrs (context . Background () , 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 ))
546+ if err := http .NewResponseController (fc .responseWriter ).Flush (); err != nil && logger . Enabled ( ctx , slog . LevelWarn ) {
547+ 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 ))
532548 }
533549
534550 return false
535551}
536552
537553//export go_read_post
538554func go_read_post (threadIndex C.uintptr_t , cBuf * C.char , countBytes C.size_t ) (readBytes C.size_t ) {
539- fc := phpThreads [threadIndex ].getRequestContext ( )
555+ fc := phpThreads [threadIndex ].context (). Value ( contextKey ).( * frankenPHPContext )
540556
541557 if fc .responseWriter == nil {
542558 return 0
@@ -555,7 +571,7 @@ func go_read_post(threadIndex C.uintptr_t, cBuf *C.char, countBytes C.size_t) (r
555571
556572//export go_read_cookies
557573func go_read_cookies (threadIndex C.uintptr_t ) * C.char {
558- request := phpThreads [threadIndex ].getRequestContext ( ).request
574+ request := phpThreads [threadIndex ].context (). Value ( contextKey ).( * frankenPHPContext ).request
559575 if request == nil {
560576 return nil
561577 }
@@ -599,7 +615,7 @@ func go_log(message *C.char, level C.int) {
599615
600616//export go_is_context_done
601617func go_is_context_done (threadIndex C.uintptr_t ) C.bool {
602- return C .bool (phpThreads [threadIndex ].getRequestContext ( ).isDone )
618+ return C .bool (phpThreads [threadIndex ].context (). Value ( contextKey ).( * frankenPHPContext ).isDone )
603619}
604620
605621// ExecuteScriptCLI executes the PHP script passed as parameter.
0 commit comments