Skip to content

Commit

Permalink
doc: modernize code examples in the cluster.md
Browse files Browse the repository at this point in the history
- 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]>
  • Loading branch information
vsemozhetbyt authored and evanlucas committed Jan 4, 2017
1 parent a5a738c commit 9044423
Showing 1 changed file with 25 additions and 20 deletions.
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) {
cluster.workers[id].on('message', messageHandler);
});
}

} else {

Expand Down Expand Up @@ -287,8 +292,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 @@ -304,7 +309,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 @@ -430,7 +435,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 @@ -526,7 +531,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 @@ -590,7 +595,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 @@ -809,7 +814,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 @@ -823,7 +828,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

0 comments on commit 9044423

Please sign in to comment.