From c9c9b5c47eb82c118391ee4a37ddd5d5fdb86908 Mon Sep 17 00:00:00 2001 From: Vse Mozhet Byt Date: Wed, 14 Dec 2016 23:46:08 +0200 Subject: [PATCH] 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 Reviewed-By: Sam Roberts --- doc/api/cluster.md | 45 +++++++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/doc/api/cluster.md b/doc/api/cluster.md index c323282d0f94b1..dc8b6ec6a2b220 100644 --- a/doc/api/cluster.md +++ b/doc/api/cluster.md @@ -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(); } @@ -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 @@ -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 { @@ -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'); @@ -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 }); @@ -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) { @@ -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 ...'); } @@ -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; @@ -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]); } } @@ -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]; }); ```