mirror of
https://github.com/nodejs/node.git
synced 2024-12-01 16:10:02 +01:00
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>
This commit is contained in:
parent
a1652324cd
commit
e03ee719e6
@ -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 {
|
||||
|
||||
@ -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');
|
||||
@ -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
|
||||
});
|
||||
|
||||
@ -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) {
|
||||
@ -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 ...');
|
||||
}
|
||||
@ -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;
|
||||
@ -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]);
|
||||
}
|
||||
}
|
||||
@ -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];
|
||||
});
|
||||
```
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user