Repository navigation
11 built in worker pool inside httpserver replace user level threadpool boilerplate - #12
Merged
EdmondDantes merged 5 commits intoMay 6, 2026
Conversation
Adds HttpServerConfig::setWorkers(N) / getWorkers(). When workers > 1, HttpServer::start() builds an Async\ThreadPool(N), submits N copies of a worker stub closure that transfers $this to each worker thread, and awaits every worker's standalone start(). Each worker re-binds the configured listeners; the kernel load-balances accept() via SO_REUSEPORT. The pool stub is a tiny user-land closure built via zend_eval_string — internal methods have no op_array, so the array-callable form [$this, 'start'] crashes the cross-thread snapshot serialiser. Default setWorkers(1) keeps current behaviour bit-for-bit. Caveats (documented): - $server->stop() on the pool-parent throws: Async\ThreadPool::cancel() doesn't wake workers suspended on their own server's wait event. Cross-thread shutdown is a follow-up. - SO_REUSEPORT is Linux/BSD-only; Windows libuv falls back to single accept thread. phpt: tests/phpt/server/core/020-builtin-worker-pool.phpt verifies the spin-up + serve path.
conn->drain_not_before_ns is computed from zend_hrtime() (CLOCK_MONOTONIC_RAW on Linux). The dispose path's fallback used http_now_coarse_ns() (CLOCK_MONOTONIC_COARSE) when CoDel and telemetry were both disabled — the two clocks drift by minutes after suspend / NTP slewing (observed: ~35 min on a long-running WSL2 VM), so the drain check mis-fired on the first response. Same fix in the H2 commit path, which had the identical pattern. H3 already used zend_hrtime() directly. tests/phpt/server/h1/010-drain-proactive-age-h1.phpt was failing on machines with non-trivial RAW/COARSE skew; passes consistently now.
Replaces the placeholder Async\ThreadPool/zend_eval_string-driven worker pool path with a direct C-handler submission: * New pool_worker_handler — runs on the worker thread, LOAD's the parent-transferred HttpServer zval, calls $server->start(), releases worker-local zval. No PHP closure, no op_array snapshot. * One persistent shell per worker, allocated as a single pemalloc'd array on the parent server. Lifetime is the parent's: http_server_free releases the array. Snapshot deep-copy isn't concurrency-safe, so workers cannot share a single shell. * Parent suspends on a wait_event; each worker's completion callback decrements a pending counter and notifies when it hits zero. Mid-loop submit failure marks remaining slots as already done so previously-submitted workers' callbacks land on a still -valid state — no UAF on partial failure. * in_pool_mode bool replaces the previous worker_pool zval — parent in pool mode has no listen events, so stop() refuses with a clear message until cross-thread shutdown lands. Requires php-src true-async / true-async-stable + php-async companion commits with the new submit_internal API and top-level transfer/load helpers.
If submit_internal fails on the very first iteration we end up with pending=0 and no callbacks registered to ever notify all_done — the parent's SUSPEND() would deadlock. Gate the suspend on pending > 0; when nothing got submitted, just propagate the failure through the return code.
…anual layout multi-worker.php is now the recommended preforking example — uses HttpServerConfig::setWorkers(N), one HttpServer in the script, the parent's start() blocks on its internal pool. No more user-level Async\ThreadPool boilerplate. The previous user-driven layout is preserved as multi-worker-manual.php — it stays useful when you need a per-worker init step that runs before listeners come up (opcache warm-up, DB pool bootstrap, custom fixtures), since setWorkers() doesn't expose a worker-init hook yet. README quick-start shows setWorkers(N) opt-in; USAGE.md multi-worker section + 'Where to look next' updated to reference both example files.
Contributor
CoverageTotal lines: 80.89% → 80.10% (-0.79 pp)
❌ Regression in touched files (> 1.0 pp drop)
Add |
EdmondDantes
deleted the
11-built-in-worker-pool-inside-httpserver-replace-user-level-threadpool-boilerplate
branch
May 6, 2026 14:52
EdmondDantes
added a commit
that referenced
this pull request
May 8, 2026
#8 Drop the prefixed[PATH_MAX] scratch in http_static_path_resolve. validate_segments already tolerates either a leading '/' or none. #9 http_static_path_is_hidden no longer memcpy's `relative` into a 4 KiB scratch buffer — http_static_path_resolve writes the NUL right after `relative` inside out_buf, so fnmatch can read it directly. #12 MIME header comment now describes override-wins semantics (mime_overrides consulted before the built-in table) instead of the inverted version. #13 ss_state_t.fs_path drops from char[PATH_MAX] to emalloc'd char *. New ss_state_free helper centralises ownership. With 10 K concurrent in-flight static requests this saves up to 40 MiB of working set. #15 canonicalise_root_directory rejects realpath() == "/" up front; previously a root="/" StaticHandler would silently serve nothing because resolved_under_root needs canonical[root_len] in {NUL,'/'}.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.