Skip to content

Commit 19bcd4a

Browse files
committed
chore: continuing with chapter 12
1 parent 5400793 commit 19bcd4a

4 files changed

Lines changed: 565 additions & 0 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# 03-http-cluster-resilient
2+
3+
This example demonstrates how to benchmark a simple HTTP server that uses the
4+
`node:cluster` module and provides auto restarting of failed workers.
5+
6+
## Dependencies
7+
8+
This example requires you to install some third-party dependencies from npm.
9+
10+
If you have `pnpm` installed, you can do that with:
11+
12+
```bash
13+
pnpm install
14+
```
15+
16+
Alternatively, if you prefer to use another package manager, make sure to delete
17+
the `pnpm-lock.yaml` file before using it.
18+
19+
If you want to use `npm`, you can run:
20+
21+
```bash
22+
npm install
23+
```
24+
25+
If you want to use `yarn`, you can run:
26+
27+
```bash
28+
yarn install
29+
```
30+
31+
## Run
32+
33+
To run the example server:
34+
35+
```bash
36+
npm start # or `node app.js`
37+
```
38+
39+
To run a benchmark (in another terminal):
40+
41+
```bash
42+
npm run benchmark # or `npx autocannon http://localhost:8080`
43+
```
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import cluster from 'node:cluster'
2+
import { createServer } from 'node:http'
3+
import { cpus } from 'node:os'
4+
5+
if (cluster.isPrimary) {
6+
const availableCpus = cpus()
7+
console.log(`Clustering to ${availableCpus.length} processes`)
8+
for (const _ of availableCpus) {
9+
cluster.fork()
10+
}
11+
cluster.on('exit', (worker, code) => {
12+
if (code !== 0 && !worker.exitedAfterDisconnect) {
13+
console.log(`Worker ${worker.process.pid} crashed. Starting a new worker`)
14+
cluster.fork()
15+
}
16+
})
17+
} else {
18+
setInterval(
19+
() => {
20+
if (Math.random() < 0.5) {
21+
throw new Error(`Ooops... ${process.pid} crashed!`)
22+
}
23+
},
24+
Math.ceil(Math.random() * 3) * 1000
25+
)
26+
const server = createServer((_req, res) => {
27+
// simulates CPU intensive work
28+
let i = 1e7
29+
while (i > 0) {
30+
i--
31+
}
32+
33+
console.log(`Handling request from ${process.pid}`)
34+
res.end(`Hello from ${process.pid}\n`)
35+
})
36+
37+
server.listen(8080, () => console.log(`Started at ${process.pid}`))
38+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "03-http-cluster-resilient",
3+
"version": "1.0.0",
4+
"description": "This example demonstrates how to benchmark a simple HTTP server that uses the `node:cluster` module and provides auto restarting of failed workers.",
5+
"type": "module",
6+
"scripts": {
7+
"start": "node app.js",
8+
"benchmark": "autocannon http://localhost:8080"
9+
},
10+
"engines": {
11+
"node": ">=24"
12+
},
13+
"engineStrict": true,
14+
"keywords": [],
15+
"author": "Luciano Mammino and Mario Casciaro",
16+
"license": "MIT",
17+
"devDependencies": {
18+
"autocannon": "^8.0.0"
19+
}
20+
}

0 commit comments

Comments
 (0)