feat: expose thread API for Go-based PHP extensions#2305
feat: expose thread API for Go-based PHP extensions#2305johanjanssens wants to merge 2 commits intophp:mainfrom
Conversation
Add APIs that enable Go-based PHP extensions to execute on existing FrankenPHP threads and access per-request state: - Thread(index) — retrieves a PHP thread by its index, returning its *http.Request which carries the request context - PHPThread.Pin() — pins a Go object to prevent GC collection - PHPThread.IsRequestDone() — checks if the request has been closed - frankenphp_thread_index() — C function returning the current thread index so C extension methods can call back into Go This makes it possible to build PHP extensions where PHP calls into C, C resolves the current thread index, and Go handles the logic with full access to the request context.
|
@dunglas These are the key changes required for: |
dunglas
left a comment
There was a problem hiding this comment.
LGTM, but could you add some tests please?
| } | ||
|
|
||
| // PHPThread exposes a PHP thread's request context. | ||
| type PHPThread struct { |
There was a problem hiding this comment.
Maybe we could use this new type internally too, for code clarity?
There was a problem hiding this comment.
Agreed. Kept it minimal to avoid a large refactor.
We can definitely take this further. Need to define scope a bit. For example, if you push this deeper I would suggest to consider addressing worker context, at the moment a worker carries a dummy context at boot time.
There's no way to inject persistent context from outside (e.g. middleware-style state that lives across requests on a worker thread). Could look into making PHPThread carry context consistently for both regular and worker threads.
Happy to expand this PR, or follow up in a new one. A context refactor would touch quite a bit more code.
Test Thread(), PHPThread.Pin(), and request handling. Fix nil pointer dereference in Thread() when no request is active or index is out of bounds.
|
Tests added. |
Add APIs that enable Go-based PHP extensions to execute on existing FrankenPHP threads and access per-request state:
Thread(index): retrieves a PHP thread by its index, returning its *http.Request which carries the request contextPHPThread.Pin(): pins a Go object to prevent GC collectionPHPThread.IsRequestDone(): checks if the request has been closedfrankenphp_thread_index(): C function returning the current thread index so C extension methods can call back into GoThis makes it possible to build PHP extensions where PHP calls into C, C resolves the current thread index, and Go handles the logic with full access to the request context.