@@ -274,7 +274,7 @@ func Init(options ...Option) error {
274274 return err
275275 }
276276
277- regularRequestChan = make (chan context. Context , opt .numThreads - workerThreadCount )
277+ regularRequestChan = make (chan contextHolder , opt .numThreads - workerThreadCount )
278278 regularThreads = make ([]* phpThread , 0 , opt .numThreads - workerThreadCount )
279279 for i := 0 ; i < opt .numThreads - workerThreadCount ; i ++ {
280280 convertToRegularThread (getInactivePHPThread ())
@@ -346,6 +346,8 @@ func ServeHTTP(responseWriter http.ResponseWriter, request *http.Request) error
346346 ctx := request .Context ()
347347 fc , ok := fromContext (ctx )
348348
349+ ch := contextHolder {ctx , fc }
350+
349351 if ! ok {
350352 return ErrInvalidRequest
351353 }
@@ -358,17 +360,17 @@ func ServeHTTP(responseWriter http.ResponseWriter, request *http.Request) error
358360
359361 // Detect if a worker is available to handle this request
360362 if fc .worker != nil {
361- return fc .worker .handleRequest (ctx )
363+ return fc .worker .handleRequest (ch )
362364 }
363365
364366 // If no worker was available, send the request to non-worker threads
365- return handleRequestWithRegularPHPThreads (ctx )
367+ return handleRequestWithRegularPHPThreads (ch )
366368}
367369
368370//export go_ub_write
369371func go_ub_write (threadIndex C.uintptr_t , cBuf * C.char , length C.int ) (C.size_t , C.bool ) {
370- ctx := phpThreads [threadIndex ]. context ()
371- fc := ctx . Value ( contextKey ).( * frankenPHPContext )
372+ thread := phpThreads [threadIndex ]
373+ fc := thread . frankenPHPContext ( )
372374
373375 if fc .isDone {
374376 return 0 , C .bool (true )
@@ -383,14 +385,27 @@ func go_ub_write(threadIndex C.uintptr_t, cBuf *C.char, length C.int) (C.size_t,
383385 writer = fc .responseWriter
384386 }
385387
388+ var ctx context.Context
389+
386390 i , e := writer .Write (unsafe .Slice ((* byte )(unsafe .Pointer (cBuf )), length ))
387- if e != nil && fc .logger .Enabled (ctx , slog .LevelWarn ) {
388- fc .logger .LogAttrs (ctx , slog .LevelWarn , "write error" , slog .Any ("error" , e ))
391+ if e != nil {
392+ ctx = thread .context ()
393+
394+ if fc .logger .Enabled (ctx , slog .LevelWarn ) {
395+ fc .logger .LogAttrs (ctx , slog .LevelWarn , "write error" , slog .Any ("error" , e ))
396+ }
389397 }
390398
391- if fc .responseWriter == nil && fc . logger . Enabled ( ctx , slog . LevelInfo ) {
399+ if fc .responseWriter == nil {
392400 // probably starting a worker script, log the output
393- fc .logger .LogAttrs (ctx , slog .LevelInfo , writer .(* bytes.Buffer ).String ())
401+
402+ if ctx == nil {
403+ ctx = thread .context ()
404+ }
405+
406+ if fc .logger .Enabled (ctx , slog .LevelInfo ) {
407+ fc .logger .LogAttrs (ctx , slog .LevelInfo , writer .(* bytes.Buffer ).String ())
408+ }
394409 }
395410
396411 return C .size_t (i ), C .bool (fc .clientHasClosed ())
@@ -400,7 +415,7 @@ func go_ub_write(threadIndex C.uintptr_t, cBuf *C.char, length C.int) (C.size_t,
400415func go_apache_request_headers (threadIndex C.uintptr_t ) (* C.go_string , C.size_t ) {
401416 thread := phpThreads [threadIndex ]
402417 ctx := thread .context ()
403- fc := ctx . Value ( contextKey ).( * frankenPHPContext )
418+ fc := thread . frankenPHPContext ( )
404419
405420 if fc .responseWriter == nil {
406421 // worker mode, not handling a request
@@ -477,13 +492,12 @@ func splitRawHeader(rawHeader *C.char, length int) (string, string) {
477492
478493//export go_write_headers
479494func go_write_headers (threadIndex C.uintptr_t , status C.int , headers * C.zend_llist ) C.bool {
480- ctx := phpThreads [threadIndex ].context ()
481- if ctx == nil {
495+ thread := phpThreads [threadIndex ]
496+ fc := thread .frankenPHPContext ()
497+ if fc == nil {
482498 return C .bool (false )
483499 }
484500
485- fc := ctx .Value (contextKey ).(* frankenPHPContext )
486-
487501 if fc .isDone {
488502 return C .bool (false )
489503 }
@@ -506,6 +520,8 @@ func go_write_headers(threadIndex C.uintptr_t, status C.int, headers *C.zend_lli
506520 // go panics on invalid status code
507521 // https://github.com/golang/go/blob/9b8742f2e79438b9442afa4c0a0139d3937ea33f/src/net/http/server.go#L1162
508522 if goStatus < 100 || goStatus > 999 {
523+ ctx := thread .context ()
524+
509525 if logger .Enabled (ctx , slog .LevelWarn ) {
510526 logger .LogAttrs (ctx , slog .LevelWarn , "Invalid response status code" , slog .Int ("status_code" , goStatus ))
511527 }
@@ -528,13 +544,12 @@ func go_write_headers(threadIndex C.uintptr_t, status C.int, headers *C.zend_lli
528544
529545//export go_sapi_flush
530546func go_sapi_flush (threadIndex C.uintptr_t ) bool {
531- ctx := phpThreads [threadIndex ].context ()
532- if ctx == nil {
547+ thread := phpThreads [threadIndex ]
548+ fc := thread .frankenPHPContext ()
549+ if fc == nil {
533550 return false
534551 }
535552
536- fc := ctx .Value (contextKey ).(* frankenPHPContext )
537-
538553 if fc .responseWriter == nil {
539554 return false
540555 }
@@ -543,16 +558,20 @@ func go_sapi_flush(threadIndex C.uintptr_t) bool {
543558 return true
544559 }
545560
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 ))
561+ if err := http .NewResponseController (fc .responseWriter ).Flush (); err != nil {
562+ ctx := thread .context ()
563+
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 ))
566+ }
548567 }
549568
550569 return false
551570}
552571
553572//export go_read_post
554573func go_read_post (threadIndex C.uintptr_t , cBuf * C.char , countBytes C.size_t ) (readBytes C.size_t ) {
555- fc := phpThreads [threadIndex ].context (). Value ( contextKey ).( * frankenPHPContext )
574+ fc := phpThreads [threadIndex ].frankenPHPContext ( )
556575
557576 if fc .responseWriter == nil {
558577 return 0
@@ -571,7 +590,7 @@ func go_read_post(threadIndex C.uintptr_t, cBuf *C.char, countBytes C.size_t) (r
571590
572591//export go_read_cookies
573592func go_read_cookies (threadIndex C.uintptr_t ) * C.char {
574- request := phpThreads [threadIndex ].context (). Value ( contextKey ).( * frankenPHPContext ).request
593+ request := phpThreads [threadIndex ].frankenPHPContext ( ).request
575594 if request == nil {
576595 return nil
577596 }
@@ -615,7 +634,7 @@ func go_log(message *C.char, level C.int) {
615634
616635//export go_is_context_done
617636func go_is_context_done (threadIndex C.uintptr_t ) C.bool {
618- return C .bool (phpThreads [threadIndex ].context (). Value ( contextKey ).( * frankenPHPContext ).isDone )
637+ return C .bool (phpThreads [threadIndex ].frankenPHPContext ( ).isDone )
619638}
620639
621640// ExecuteScriptCLI executes the PHP script passed as parameter.
0 commit comments