Node Cluster is not dispatching task to another worker available

982 Views Asked by At

This is my first question of Stack-overflow so please pardon me for any mistake or insufficient information in this question.

So, I am trying to use cluster module of nodeJS for my server and I run nodeJS through my windows machine. I know nodeJS does not have any scheduling policy for cluster module in windows so I have explicitly set the scheduling_policy to rr as mentioned by nodeJS docs. But the problem is when I am trying to keep one worker busy by putting it in an infinite loop; server is not dispatching the request to another worker available and free when we tried to request the server for '/' resource.

Please help me why it is not dispatching the request to other workers.

var cluster=require('cluster');
if(cluster.isMaster){
 var cores=require('os').cpus().length;
 console.log("Master Cluster setting up :-"+cores+" workers");
 for(var i=0;i<cores;i++)
  cluster.fork();
 cluster.on('online',(worker)=>{
  console.log("Worker with Process ID :- "+worker.process.pid+" online");
 });
 cluster.on('exit',(worker)=>{
  console.log("worker "+worker.process.pid+" died...So setting up a new worker");
  cluster.fork();
 });
}
else{
 var app=require('express')();
 app.get('/',(req,res)=>{
  console.log("Process with pid "+process.pid+" is handling this request");
  while(true);
  res.write("Yes!");
  res.end();
  //while(true);
 })
 app.listen('3000');
}

1

There are 1 best solutions below

0
On BEST ANSWER

The above code you written working good. But you need to send concurrent requests to your node server instead of sending requesting in loop then, you will see the power of node cluster module.

Node cluster module has two approaches for distributing incoming connections.

The first one (and the default one on all platforms except Windows), is the round-robin approach.

The second approach is where the master process creates the listen socket and sends it to interested workers. The workers then accept incoming connections directly

You can use http://blog.remarkablelabs.com/2012/11/benchmarking-and-load-testing-with-siege for sending concurrent request.Now you will see switching to your process.