Browse Source

doc: modernize code examples in the cluster.md

- Fixes https://github.com/nodejs/node/issues/10255
- It also will be consistent with a previous code example.
- `cluster.workers` iteration: `Object.keys().forEach` -> `for`...`in`

PR-URL: https://github.com/nodejs/node/pull/10270
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Sam Roberts <vieuxtech@gmail.com>
v6
Vse Mozhet Byt 8 years ago
committed by Julian Duque
parent
commit
e03ee719e6
No known key found for this signature in database GPG Key ID: D2F1394777BDB2E2
  1. 45
      doc/api/cluster.md

45
doc/api/cluster.md

@ -15,8 +15,10 @@ const http = require('http');
const numCPUs = require('os').cpus().length; const numCPUs = require('os').cpus().length;
if (cluster.isMaster) { if (cluster.isMaster) {
console.log(`Master ${process.pid} is running`);
// Fork workers. // Fork workers.
for (var i = 0; i < numCPUs; i++) { for (let i = 0; i < numCPUs; i++) {
cluster.fork(); cluster.fork();
} }
@ -30,17 +32,20 @@ if (cluster.isMaster) {
res.writeHead(200); res.writeHead(200);
res.end('hello world\n'); res.end('hello world\n');
}).listen(8000); }).listen(8000);
console.log(`Worker ${process.pid} started`);
} }
``` ```
Running Node.js will now share port 8000 between the workers: Running Node.js will now share port 8000 between the workers:
```txt ```txt
$ NODE_DEBUG=cluster node server.js $ node server.js
23521,Master Worker 23524 online Master 3596 is running
23521,Master Worker 23526 online Worker 4324 started
23521,Master Worker 23523 online Worker 4520 started
23521,Master Worker 23528 online Worker 6056 started
Worker 5644 started
``` ```
Please note that on Windows, it is not yet possible to set up a named pipe Please note that on Windows, it is not yet possible to set up a named pipe
@ -202,27 +207,27 @@ const http = require('http');
if (cluster.isMaster) { if (cluster.isMaster) {
// Keep track of http requests // Keep track of http requests
var numReqs = 0; let numReqs = 0;
setInterval(() => { setInterval(() => {
console.log('numReqs =', numReqs); console.log(`numReqs = ${numReqs}`);
}, 1000); }, 1000);
// Count requests // Count requests
function messageHandler(msg) { function messageHandler(msg) {
if (msg.cmd && msg.cmd == 'notifyRequest') { if (msg.cmd && msg.cmd === 'notifyRequest') {
numReqs += 1; numReqs += 1;
} }
} }
// Start workers and listen for messages containing notifyRequest // Start workers and listen for messages containing notifyRequest
const numCPUs = require('os').cpus().length; const numCPUs = require('os').cpus().length;
for (var i = 0; i < numCPUs; i++) { for (let i = 0; i < numCPUs; i++) {
cluster.fork(); cluster.fork();
} }
Object.keys(cluster.workers).forEach((id) => { for (const id in cluster.workers) {
cluster.workers[id].on('message', messageHandler); cluster.workers[id].on('message', messageHandler);
}); }
} else { } else {
@ -287,8 +292,8 @@ the `'disconnect'` event has not been emitted after some time.
```js ```js
if (cluster.isMaster) { if (cluster.isMaster) {
var worker = cluster.fork(); const worker = cluster.fork();
var timeout; let timeout;
worker.on('listening', (address) => { worker.on('listening', (address) => {
worker.send('shutdown'); worker.send('shutdown');
@ -304,7 +309,7 @@ if (cluster.isMaster) {
} else if (cluster.isWorker) { } else if (cluster.isWorker) {
const net = require('net'); const net = require('net');
var server = net.createServer((socket) => { const server = net.createServer((socket) => {
// connections never end // connections never end
}); });
@ -430,7 +435,7 @@ This example will echo back all messages from the master:
```js ```js
if (cluster.isMaster) { if (cluster.isMaster) {
var worker = cluster.fork(); const worker = cluster.fork();
worker.send('hi there'); worker.send('hi there');
} else if (cluster.isWorker) { } else if (cluster.isWorker) {
@ -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. This can be used to log worker activity, and create your own timeout.
```js ```js
var timeouts = []; const timeouts = [];
function errorMsg() { function errorMsg() {
console.error('Something must be wrong with the connection ...'); console.error('Something must be wrong with the connection ...');
} }
@ -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: you can work around the discrepancy by checking the number of arguments:
```js ```js
cluster.on('message', function(worker, message, handle) { cluster.on('message', (worker, message, handle) => {
if (arguments.length === 2) { if (arguments.length === 2) {
handle = message; handle = message;
message = worker; message = worker;
@ -809,7 +814,7 @@ before last `'disconnect'` or `'exit'` event is emitted.
```js ```js
// Go through all workers // Go through all workers
function eachWorker(callback) { function eachWorker(callback) {
for (var id in cluster.workers) { for (const id in cluster.workers) {
callback(cluster.workers[id]); callback(cluster.workers[id]);
} }
} }
@ -823,7 +828,7 @@ the worker's unique id is the easiest way to find the worker.
```js ```js
socket.on('data', (id) => { socket.on('data', (id) => {
var worker = cluster.workers[id]; const worker = cluster.workers[id];
}); });
``` ```

Loading…
Cancel
Save