A modern Node.js 'cluster' example

An example of running cluster on Node 14, including w/ ESM & semantic updates for ES2020

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import cluster from 'cluster';
import {createServer} from 'http';
import {cpus} from 'os';

const isMainCluster = cluster.isMaster;
const {length:CPUCount} = cpus();

if (isMainCluster) {
console.log(`Main ${process.pid} is running`);

// Fork workers.
for (let currentCPU = 0; currentCPU < CPUCount; currentCPU++)
cluster.fork();

cluster.on('exit', worker =>
console.log(`worker ${worker.process.pid} died`));
} else {
// Workers can share any TCP connection
// In this case it is an HTTP server
createServer((req, res) => {
res.writeHead(200);
res.end('hello world\n');
}).listen(8000);

console.log(`Worker ${process.pid} started`);
}

Changes from the current Node.js ‘cluster’ docs include:

  1. Using import over require
  2. Using named imports over the default where feasible
  3. Using clearer, more semantic identifier names, e.g. currentCPU v. i
  4. Replacing archaic terminology, e.g. Master w/ Main

Output w/ ESM Node code w/ syntactical & semantic updates

1
2
3
4
5
6
7
8
9
10
11
12
13
14
> $ node index.mjs                                                                       ⬡ 14.15.1 [±main ●]
Main 57290 is running
Worker 57292 started
Worker 57295 started
Worker 57291 started
Worker 57294 started
Worker 57293 started
Worker 57296 started
Worker 57298 started
Worker 57297 started
Worker 57299 started
Worker 57300 started
Worker 57302 started
Worker 57301 started

See here for the full repo