-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f2cfa15
commit e686c2e
Showing
2 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,66 @@ | ||
--- | ||
name: Start a cluster of HTTP servers | ||
description: Run multiple concurrent HTTP servers via the "reusePort" option to share the same port across multiple processes | ||
--- | ||
|
||
To run multiple concurrent HTTP servers, use the `reusePort` option in `Bun.serve()` to share the same port across multiple processes. | ||
|
||
This automatically load balances incoming requests across multiple instances of Bun. | ||
|
||
```ts#server.ts | ||
import { serve } from "bun"; | ||
|
||
const id = = Math.random().toString(36).slice(2); | ||
|
||
serve({ | ||
port: process.env.PORT || 8080, | ||
development: false, | ||
|
||
// Share the same port across multiple processes | ||
// This is the important part! | ||
reusePort: true, | ||
|
||
async fetch(request) { | ||
return new Response("Hello from Bun #" + id + "!\n"); | ||
} | ||
}); | ||
``` | ||
|
||
--- | ||
|
||
{% callout %} | ||
**Linux only** — Windows and macOS ignore the `reusePort` option. This is an operating system limitation with `SO_REUSEPORT`, unfortunately. | ||
{% /callout %} | ||
|
||
After saving the file, start your servers on the same port. | ||
|
||
Under the hood, this uses the Linux `SO_REUSEPORT` and `SO_REUSEADDR` socket options to ensure fair load balancing across multiple processes. [Learn more about `SO_REUSEPORT` and `SO_REUSEADDR`](https://lwn.net/Articles/542629/) | ||
|
||
```ts#cluster.ts | ||
import { spawn } from "bun"; | ||
|
||
const cpus = navigator.hardwareConcurrency; // Number of CPU cores | ||
const buns = new Array(cpus); | ||
|
||
for (let i = 0; i < cpus; i++) { | ||
buns[i] = spawn({ | ||
cmd: ["bun", "./server.ts"], | ||
stdout: "inherit", | ||
stderr: "inherit", | ||
stdin: "inherit", | ||
}); | ||
} | ||
|
||
function kill() { | ||
for (const bun of buns) { | ||
bun.kill(); | ||
} | ||
} | ||
|
||
process.on("SIGINT", kill); | ||
process.on("exit", kill); | ||
``` | ||
|
||
--- | ||
|
||
At the time of writing, Bun hasn't implemented the `node:cluster` module yet, but this is a faster, simple, and limited alternative. We will also implement `node:cluster` in the future. |