Skip to content

Instantly share code, notes, and snippets.

@rmar72
Created July 14, 2019 03:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rmar72/0353f849e87727e591f0b74a220b27d5 to your computer and use it in GitHub Desktop.
Save rmar72/0353f849e87727e591f0b74a220b27d5 to your computer and use it in GitHub Desktop.
Enabling the round-robin scheduler algo, since Win doesn't enable it by default
const cluster = require('cluster');
const http = require('http');
const numCPUs = require('os').cpus().length;
cluster.schedulingPolicy = cluster.SCHED_RR; // enables the round-robin scheduler algo, since Win doesn't enable it by default
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`);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment