From a6898eca51dda10040ad17049d840236615d49ed Mon Sep 17 00:00:00 2001 From: Vse Mozhet Byt Date: Wed, 14 Dec 2016 23:46:08 +0200 Subject: [PATCH 1/7] doc: var -> let / const in the cluster.md --- doc/api/cluster.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/doc/api/cluster.md b/doc/api/cluster.md index c323282d0f94b1..8879cdc166028d 100644 --- a/doc/api/cluster.md +++ b/doc/api/cluster.md @@ -16,7 +16,7 @@ const numCPUs = require('os').cpus().length; if (cluster.isMaster) { // Fork workers. - for (var i = 0; i < numCPUs; i++) { + for (let i = 0; i < numCPUs; i++) { cluster.fork(); } @@ -202,7 +202,7 @@ const http = require('http'); if (cluster.isMaster) { // Keep track of http requests - var numReqs = 0; + let numReqs = 0; setInterval(() => { console.log('numReqs =', numReqs); }, 1000); @@ -216,7 +216,7 @@ if (cluster.isMaster) { // 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(); } @@ -285,8 +285,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 +302,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 +428,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 +524,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 ...'); } @@ -821,7 +821,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]; }); ``` From 3d13cf313e405f3ddecce997da25e0f18542873b Mon Sep 17 00:00:00 2001 From: Vse Mozhet Byt Date: Wed, 14 Dec 2016 23:53:12 +0200 Subject: [PATCH 2/7] doc: concatenate -> template literal in cluster.md --- doc/api/cluster.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/api/cluster.md b/doc/api/cluster.md index 8879cdc166028d..94b34d9f8f1a81 100644 --- a/doc/api/cluster.md +++ b/doc/api/cluster.md @@ -204,7 +204,7 @@ if (cluster.isMaster) { // Keep track of http requests let numReqs = 0; setInterval(() => { - console.log('numReqs =', numReqs); + console.log(`numReqs = ${numReqs}`); }, 1000); // Count requests From 2c1998db89a7d621f29eafd3774622820258b0bb Mon Sep 17 00:00:00 2001 From: Vse Mozhet Byt Date: Wed, 14 Dec 2016 23:54:49 +0200 Subject: [PATCH 3/7] doc: == -> === in the cluster.md --- doc/api/cluster.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/api/cluster.md b/doc/api/cluster.md index 94b34d9f8f1a81..e3e7a3c6847144 100644 --- a/doc/api/cluster.md +++ b/doc/api/cluster.md @@ -209,7 +209,7 @@ if (cluster.isMaster) { // Count requests function messageHandler(msg) { - if (msg.cmd && msg.cmd == 'notifyRequest') { + if (msg.cmd && msg.cmd === 'notifyRequest') { numReqs += 1; } } From ef60ed52f6daa8e7c0313aaafa0dc35d854d850e Mon Sep 17 00:00:00 2001 From: Vse Mozhet Byt Date: Thu, 15 Dec 2016 00:00:17 +0200 Subject: [PATCH 4/7] doc: anonymous -> arrow function in the cluster.md --- doc/api/cluster.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/api/cluster.md b/doc/api/cluster.md index e3e7a3c6847144..aa035d71866145 100644 --- a/doc/api/cluster.md +++ b/doc/api/cluster.md @@ -588,7 +588,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; From 473a29c8fa5c80934fd64a8275caf61b45e5a2cf Mon Sep 17 00:00:00 2001 From: Vse Mozhet Byt Date: Thu, 15 Dec 2016 00:03:36 +0200 Subject: [PATCH 5/7] doc: for...in -> forEach in the cluster.md It also will be consistent with a previous code example. --- doc/api/cluster.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/api/cluster.md b/doc/api/cluster.md index aa035d71866145..4ea0e446a63e60 100644 --- a/doc/api/cluster.md +++ b/doc/api/cluster.md @@ -807,9 +807,9 @@ before last `'disconnect'` or `'exit'` event is emitted. ```js // Go through all workers function eachWorker(callback) { - for (var id in cluster.workers) { + Object.keys(cluster.workers).forEach((id) => { callback(cluster.workers[id]); - } + }); } eachWorker((worker) => { worker.send('big announcement to all workers'); From efee9b641f17bde518f6ecd7830445b7079a4033 Mon Sep 17 00:00:00 2001 From: Vse Mozhet Byt Date: Thu, 15 Dec 2016 00:08:42 +0200 Subject: [PATCH 6/7] doc: update an obsolete example in the cluster.md Fixes: https://github.com/nodejs/node/issues/10255 --- doc/api/cluster.md | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/doc/api/cluster.md b/doc/api/cluster.md index 4ea0e446a63e60..92195d18cbb395 100644 --- a/doc/api/cluster.md +++ b/doc/api/cluster.md @@ -15,6 +15,8 @@ const http = require('http'); const numCPUs = require('os').cpus().length; if (cluster.isMaster) { + console.log(`Master ${process.pid} is running`); + // Fork workers. 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 From e5307f34329d792ee26a3e5d2b620b1e47165583 Mon Sep 17 00:00:00 2001 From: Vse Mozhet Byt Date: Thu, 15 Dec 2016 03:48:41 +0200 Subject: [PATCH 7/7] doc: simplify examples in the cluster.md `cluster.workers` iteration: `Object.keys().forEach` -> `for`...`in` --- doc/api/cluster.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/doc/api/cluster.md b/doc/api/cluster.md index 92195d18cbb395..dc8b6ec6a2b220 100644 --- a/doc/api/cluster.md +++ b/doc/api/cluster.md @@ -225,9 +225,9 @@ if (cluster.isMaster) { cluster.fork(); } - Object.keys(cluster.workers).forEach((id) => { + for (const id in cluster.workers) { cluster.workers[id].on('message', messageHandler); - }); + } } else { @@ -812,9 +812,9 @@ before last `'disconnect'` or `'exit'` event is emitted. ```js // Go through all workers function eachWorker(callback) { - Object.keys(cluster.workers).forEach((id) => { + for (const id in cluster.workers) { callback(cluster.workers[id]); - }); + } } eachWorker((worker) => { worker.send('big announcement to all workers');