-
-
Notifications
You must be signed in to change notification settings - Fork 58
feat: add cluster mode #221
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
OskarLebuda
wants to merge
9
commits into
h3js:main
Choose a base branch
from
OskarLebuda:feat/cluster-mode
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f234125
feat: add cluster mode
OskarLebuda 7fc21ac
feat: add cluster mode
OskarLebuda b1e16ee
chore: apply automated updates
autofix-ci[bot] 1df76b0
feat: add cluster mode
OskarLebuda 0beeab4
feat: add cluster mode
OskarLebuda 2e12dd9
Merge branch 'main' into feat/cluster-mode
OskarLebuda d73d203
Merge branch 'main' into feat/cluster-mode
OskarLebuda 7856edb
fix(cluster): address review feedback
OskarLebuda 5c42201
Merge branch 'main' into feat/cluster-mode
OskarLebuda File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| --- | ||
| icon: ri:stack-line | ||
| --- | ||
|
|
||
| # Cluster Mode | ||
|
|
||
| > Run multiple server processes sharing the same port | ||
|
|
||
| A single JavaScript process uses one CPU core. In production, cluster mode lets srvx spawn multiple worker processes that share the same port, so incoming requests are load balanced across all CPU cores - no external process manager needed. | ||
|
|
||
| The main process becomes a lightweight supervisor. It never handles requests itself; it spawns workers (re-executing the same server entry), restarts crashed workers (with exponential backoff) and forwards shutdown signals so each worker can close gracefully. | ||
|
|
||
| ## Usage | ||
|
|
||
| **CLI:** | ||
|
|
||
| ```bash | ||
| # One worker per CPU core | ||
| srvx serve --prod --cluster | ||
|
|
||
| # Exact number of workers | ||
| srvx serve --prod --cluster=4 | ||
| ``` | ||
|
|
||
| > [!NOTE] | ||
| > Cluster mode is production-only in the CLI (in dev mode a single process with watcher is used). | ||
|
|
||
| **Programmatic:** | ||
|
|
||
| ```js | ||
| import { serve } from "srvx"; | ||
|
|
||
| serve({ | ||
| cluster: true, // or an exact number of workers | ||
| fetch: () => new Response(`👋 Hello from worker ${process.env.SRVX_CLUSTER_WORKER}`), | ||
| }); | ||
| ``` | ||
|
|
||
| **Environment variable:** | ||
|
|
||
| Setting `SRVX_WORKERS` enables cluster mode without touching code or CLI flags (handy in Docker / Kubernetes): | ||
|
|
||
| ```bash | ||
| SRVX_WORKERS=4 srvx serve --prod | ||
| ``` | ||
|
|
||
| An explicit `cluster: <number>` option takes precedence over `SRVX_WORKERS`. Use `cluster: false` (or `--cluster=false` in the CLI) to disable cluster mode entirely, including `SRVX_WORKERS`. | ||
|
|
||
| ## Worker processes | ||
|
|
||
| Each worker re-executes the same server entry. Workers can be detected via the `SRVX_CLUSTER_WORKER` environment variable, which contains the worker index (starting at `"0"`): | ||
|
|
||
| ```js | ||
| if (process.env.SRVX_CLUSTER_WORKER) { | ||
| // Running as a cluster worker | ||
| } | ||
| ``` | ||
|
|
||
| If a worker crashes, the supervisor restarts it automatically (with exponential backoff). If a worker repeatedly fails during startup (e.g. port in use, invalid TLS config), the supervisor gives up after 3 attempts and exits with a non-zero code. | ||
|
|
||
| On `SIGINT`/`SIGTERM`, the supervisor forwards the signal to all workers for graceful shutdown and exits once all of them have finished. | ||
|
|
||
| ## Runtime support | ||
|
|
||
| - **Node.js**: workers share the listening socket via [`node:cluster`](https://nodejs.org/api/cluster.html), so load balancing works on every platform. The distribution follows Node's own scheduling policy: round-robin everywhere except Windows, where Node defaults to `SCHED_NONE` and lets the OS pick the worker (override with `NODE_CLUSTER_SCHED_POLICY=rr`). | ||
| - **Bun** and **Deno**: workers bind the port with `SO_REUSEPORT` and the kernel load balances between them. Kernel load balancing is **Linux only** - on other platforms a single supervised worker is started instead (crash restarts still work). | ||
| - **Serverless runtimes** (Cloudflare, AWS Lambda, ...): the platform scales processes itself, the `cluster` option is ignored. | ||
|
|
||
| > [!IMPORTANT] | ||
| > Cluster mode requires a fixed port (`port: 0` is not supported). |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.