Q
Q
quex2014-12-13 02:00:09
Node.js
quex, 2014-12-13 02:00:09

Do I understand correctly how cluster works in NodeJS?

var http = require('http');
var cluster = require('cluster');

var server = http.createServer();
server.on('request', ...);
server.on('error', ...);

var rand = Math.random();

if (cluster.isMaster){
    var i = 4;
    while(i--) cluster.fork();
}
else {
    console.log(rand);
    server.listen(8888);
}

Such a script works without errors, when I run it, I see 4 different numbers in the console. This begs the question, do I understand correctly that when cluster.fork() is called, the following happens:
1. A complete copy of the current process is created (it is called with exactly the same arguments, etc., with which it was called by the user initially). Roughly speaking, as if the user repeated the last command in the console.
2. In some way, a command is given to the process so that it understands that the behavior of the cluster module inside the code needs to be changed appropriately, letting it know that it is no longer in the role of master. (cluster.isWorker = true, cluster.isMaster = false, etc.).
As a result, the following instructions are actually executed.
var http = require('http');
var cluster = require('cluster');
var server = http.createServer();
server.on('request', ...);
server.on('error', ...);
var rand = Math.random();
console.log(rand);
server.listen(8888);

Do I understand everything correctly?
That is, it is not at all necessary to wrap the entire code of the worker in if (cluster.isWorker) { ... }?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ivan Starkov, 2014-12-13
@quex

cluster.isWorker is equal to the !cluster.isMaster node
, this is directly stated in the docs (it is the negation of cluster.isMaster) so wrapping is not necessary once there is already if(cluster.isMaster){}else
child processes understand that they are not masters by variable process.env.NODE_UNIQUE_ID if defined then I am a worker.
All this can be viewed directly in the cluster code here https://github.com/joyent/node/blob/master/lib/clu...
see function createWorkerProcess(id, env)

M
Menni, 2019-08-22
@Menni

You can create a cluster in nodejs only for the isMaster process and after the if check, spawn a child process on each server core and the number of child processes must match the number of cores. And it’s better to create a cluster in index.js which will be the entry point (npm start should run node index.js), and separately create server.js with the server creation code.
Entry point code example:

const cluster = require('cluster');
const http = require('http');
const numCPUs = require('os').cpus().length;

if (cluster.isMaster) {
  console.log(`Leader ${process.pid} is running`);

  // Fork workers.
  for (let i = 0; i < numCPUs; i++) {
    cluster.fork();
  }

  cluster.on('exit', (worker, code, signal) => {
    console.log(`worker ${worker.process.pid} died`);
  });
} else {
  require('./server.js');

  console.log(`Worker ${process.pid} started`);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question