Skip to content
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

doc: modernize code examples in the cluster.md #10270

Closed
wants to merge 7 commits into from
45 changes: 25 additions & 20 deletions doc/api/cluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ const http = require('http');
const numCPUs = require('os').cpus().length;

if (cluster.isMaster) {
console.log(`Master ${process.pid} is running`);

// Fork workers.
for (var i = 0; i < numCPUs; i++) {
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}

Expand All @@ -30,17 +32,20 @@ if (cluster.isMaster) {
res.writeHead(200);
res.end('hello world\n');
}).listen(8000);

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

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

```txt
$ NODE_DEBUG=cluster node server.js
23521,Master Worker 23524 online
23521,Master Worker 23526 online
23521,Master Worker 23523 online
23521,Master Worker 23528 online
$ node server.js
Master 3596 is running
Worker 4324 started
Worker 4520 started
Worker 6056 started
Worker 5644 started
```

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

// Keep track of http requests
var numReqs = 0;
let numReqs = 0;
setInterval(() => {
console.log('numReqs =', numReqs);
console.log(`numReqs = ${numReqs}`);
}, 1000);

// Count requests
function messageHandler(msg) {
if (msg.cmd && msg.cmd == 'notifyRequest') {
if (msg.cmd && msg.cmd === 'notifyRequest') {
numReqs += 1;
}
}

// Start workers and listen for messages containing notifyRequest
const numCPUs = require('os').cpus().length;
for (var i = 0; i < numCPUs; i++) {
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}

Object.keys(cluster.workers).forEach((id) => {
for (const id in cluster.workers) {
Copy link
Member

@lpinca lpinca Dec 15, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I think it's better to keep Object.keys here? for...in also includes non own enumerable properties.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nvm for...in is also used below.

Copy link
Contributor

@sam-github sam-github Dec 15, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are no such properties in cluster.workers.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I know, but I think the intention is to also educate in examples?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this example educates that there are no such properties, that cluster.workers is a vanilla js object underived from anything, and doesn't require complex boiler plate safe-guards to be used.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough :)

cluster.workers[id].on('message', messageHandler);
});
}

} else {

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

```js
if (cluster.isMaster) {
var worker = cluster.fork();
var timeout;
const worker = cluster.fork();
let timeout;

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

} else if (cluster.isWorker) {
const net = require('net');
var server = net.createServer((socket) => {
const server = net.createServer((socket) => {
// connections never end
});

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

```js
if (cluster.isMaster) {
var worker = cluster.fork();
const worker = cluster.fork();
worker.send('hi there');

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

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

```js
cluster.on('message', function(worker, message, handle) {
cluster.on('message', (worker, message, handle) => {
if (arguments.length === 2) {
handle = message;
message = worker;
Expand Down Expand Up @@ -807,7 +812,7 @@ before last `'disconnect'` or `'exit'` event is emitted.
```js
// Go through all workers
function eachWorker(callback) {
for (var id in cluster.workers) {
for (const id in cluster.workers) {
callback(cluster.workers[id]);
}
}
Expand All @@ -821,7 +826,7 @@ the worker's unique id is the easiest way to find the worker.

```js
socket.on('data', (id) => {
var worker = cluster.workers[id];
const worker = cluster.workers[id];
});
```

Expand Down