2

Similar questions have been asked, as to why a nodejs cluster forking only delegates to one worker on a windows machine. We know the answer is that the RR algorithm is disabled on windows, but so then, how can you enable it?

I have a hint:

1) https://stackoverflow.com/a/43971478/8804567

The Docs say " cluster.schedulingPolicy can also be set through the NODE_CLUSTER_SCHED_POLICY environment variable. Valid values are 'rr' and 'none' "

I'm wondering where exactly do I make this configuration. The code:

const cluster = require('cluster');
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();
  }

} else {
  // Workers sharing same TCP connection

  http.createServer((req, res) => {
    console.log(`worker ${process.pid} working`);
    res.writeHead(200);
    res.end('hello world\n');
  }).listen(2019);

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

Any help, guidance or comments in relevancy are very much appreciated, seriously!

Thanks community!

Ruben Martinez
  • 543
  • 4
  • 12

1 Answers1

9

Ok, so it was right in my face, but I was confused as to how to do it.

I added this line of code & now when I run it & load test it I can see a proper delegation.

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

cluster.schedulingPolicy = cluster.SCHED_RR;
...
Ruben Martinez
  • 543
  • 4
  • 12