Skip to content

Commit d38cc59

Browse files
document worker mode for extensions
1 parent d7aebed commit d38cc59

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

docs/worker.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,13 @@ $handler = static function () use ($workerServer) {
179179

180180
// ...
181181
```
182+
183+
## Worker mode for extension developers
184+
185+
### Request lifecycle
186+
187+
In worker mode FrankenPHP only goes through `RINIT` and `RSHUTDOWN` once per worker thread. In case you need to observe a worker request start and end, you can not rely on `RINIT` and `RSHUTDOWN` phases of the extension lifecycle. Instead, you are able to hook into the `sapi_module.activate` / `sapi_module.deactivate` function pointers to achieve the same effect (you can validate that the current SAPI is FrankenPHP by checking the value of `sapi_module.name`).
188+
189+
Upon entering the `frankenphp_handle_request()` function, FrankenPHP calls `sapi_deactivate()` in internals which calls the `sapi_module.deactivate` hook if initialized and finaly blocks until a request arrives. Once a request arrives for the worker to handle, FrankenPHP calls `sapi_activate()` in internals which calls the `sapi_module.activate` hook if initialized.
190+
191+
Be aware that FrankenPHP worker mode injects a dummy request to start the worker even before the first request arrives.

frankenphp.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,12 @@ static void frankenphp_release_temporary_streams() {
122122
ZEND_HASH_FOREACH_END();
123123
}
124124

125-
/* Adapted from php_request_shutdown */
125+
/* Adapted from php_request_shutdown
126+
* This mimics the RSHUTDOWN phase from PHP for the worker mode. In case you
127+
* need to observe the end of a request being handled, you can hook into the
128+
* `sapi_module.deactivate` function pointer which gets called from
129+
* `sapi_deactivate()`.
130+
* */
126131
static void frankenphp_worker_request_shutdown() {
127132
/* Flush all output buffers */
128133
zend_try { php_output_end_all(); }
@@ -170,7 +175,12 @@ void frankenphp_add_assoc_str_ex(zval *track_vars_array, char *key,
170175
add_assoc_str_ex(track_vars_array, key, keylen, val);
171176
}
172177

173-
/* Adapted from php_request_startup() */
178+
/* Adapted from php_request_startup()
179+
* This mimics the RINIT phase from PHP for the worker mode. In case you need to
180+
* observe a new request being handled, you can hook into the
181+
* `sapi_module.activate` function pointer which gets called from
182+
* `sapi_activate()`.
183+
* */
174184
static int frankenphp_worker_request_startup() {
175185
int retval = SUCCESS;
176186

0 commit comments

Comments
 (0)