Skip to content

Commit 0fae3d2

Browse files
vsemozhetbytMylesBorins
authored andcommitted
doc: modernize code examples in the cluster.md
- Fixes #10255 - It also will be consistent with a previous code example. - `cluster.workers` iteration: `Object.keys().forEach` -> `for`...`in` PR-URL: #10270 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Sam Roberts <[email protected]>
1 parent d2e3a15 commit 0fae3d2

File tree

1 file changed

+25
-20
lines changed

1 file changed

+25
-20
lines changed

doc/api/cluster.md

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ const http = require('http');
1515
const numCPUs = require('os').cpus().length;
1616

1717
if (cluster.isMaster) {
18+
console.log(`Master ${process.pid} is running`);
19+
1820
// Fork workers.
19-
for (var i = 0; i < numCPUs; i++) {
21+
for (let i = 0; i < numCPUs; i++) {
2022
cluster.fork();
2123
}
2224

@@ -30,17 +32,20 @@ if (cluster.isMaster) {
3032
res.writeHead(200);
3133
res.end('hello world\n');
3234
}).listen(8000);
35+
36+
console.log(`Worker ${process.pid} started`);
3337
}
3438
```
3539

3640
Running Node.js will now share port 8000 between the workers:
3741

3842
```txt
39-
$ NODE_DEBUG=cluster node server.js
40-
23521,Master Worker 23524 online
41-
23521,Master Worker 23526 online
42-
23521,Master Worker 23523 online
43-
23521,Master Worker 23528 online
43+
$ node server.js
44+
Master 3596 is running
45+
Worker 4324 started
46+
Worker 4520 started
47+
Worker 6056 started
48+
Worker 5644 started
4449
```
4550

4651
Please note that on Windows, it is not yet possible to set up a named pipe
@@ -202,27 +207,27 @@ const http = require('http');
202207
if (cluster.isMaster) {
203208

204209
// Keep track of http requests
205-
var numReqs = 0;
210+
let numReqs = 0;
206211
setInterval(() => {
207-
console.log('numReqs =', numReqs);
212+
console.log(`numReqs = ${numReqs}`);
208213
}, 1000);
209214

210215
// Count requests
211216
function messageHandler(msg) {
212-
if (msg.cmd && msg.cmd == 'notifyRequest') {
217+
if (msg.cmd && msg.cmd === 'notifyRequest') {
213218
numReqs += 1;
214219
}
215220
}
216221

217222
// Start workers and listen for messages containing notifyRequest
218223
const numCPUs = require('os').cpus().length;
219-
for (var i = 0; i < numCPUs; i++) {
224+
for (let i = 0; i < numCPUs; i++) {
220225
cluster.fork();
221226
}
222227

223-
Object.keys(cluster.workers).forEach((id) => {
228+
for (const id in cluster.workers) {
224229
cluster.workers[id].on('message', messageHandler);
225-
});
230+
}
226231

227232
} else {
228233

@@ -285,8 +290,8 @@ the `'disconnect'` event has not been emitted after some time.
285290

286291
```js
287292
if (cluster.isMaster) {
288-
var worker = cluster.fork();
289-
var timeout;
293+
const worker = cluster.fork();
294+
let timeout;
290295

291296
worker.on('listening', (address) => {
292297
worker.send('shutdown');
@@ -302,7 +307,7 @@ if (cluster.isMaster) {
302307

303308
} else if (cluster.isWorker) {
304309
const net = require('net');
305-
var server = net.createServer((socket) => {
310+
const server = net.createServer((socket) => {
306311
// connections never end
307312
});
308313

@@ -428,7 +433,7 @@ This example will echo back all messages from the master:
428433

429434
```js
430435
if (cluster.isMaster) {
431-
var worker = cluster.fork();
436+
const worker = cluster.fork();
432437
worker.send('hi there');
433438

434439
} else if (cluster.isWorker) {
@@ -524,7 +529,7 @@ When a new worker is forked the cluster module will emit a `'fork'` event.
524529
This can be used to log worker activity, and create your own timeout.
525530

526531
```js
527-
var timeouts = [];
532+
const timeouts = [];
528533
function errorMsg() {
529534
console.error('Something must be wrong with the connection ...');
530535
}
@@ -588,7 +593,7 @@ If you need to support older versions and don't need the worker object,
588593
you can work around the discrepancy by checking the number of arguments:
589594

590595
```js
591-
cluster.on('message', function(worker, message, handle) {
596+
cluster.on('message', (worker, message, handle) => {
592597
if (arguments.length === 2) {
593598
handle = message;
594599
message = worker;
@@ -807,7 +812,7 @@ before last `'disconnect'` or `'exit'` event is emitted.
807812
```js
808813
// Go through all workers
809814
function eachWorker(callback) {
810-
for (var id in cluster.workers) {
815+
for (const id in cluster.workers) {
811816
callback(cluster.workers[id]);
812817
}
813818
}
@@ -821,7 +826,7 @@ the worker's unique id is the easiest way to find the worker.
821826

822827
```js
823828
socket.on('data', (id) => {
824-
var worker = cluster.workers[id];
829+
const worker = cluster.workers[id];
825830
});
826831
```
827832

0 commit comments

Comments
 (0)