Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node Cluster: How to assign separate server/port to each worker?

I understand that I can use Nodes cluster module in order to create several workers all serving the same socket connection (example from docs):

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

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

  cluster.on('exit', function(worker, code, signal) {
    console.log('worker ' + worker.process.pid + ' died');
  });
} else {
  // Workers can share any TCP connection
  // In this case its a HTTP server
  http.createServer(function(req, res) {
    res.writeHead(200);
    res.end("hello world\n");
  }).listen(8000);
}

However, what if I instead of serving the same connection want each worker to run their own server, each listening on a separate port?

like image 538
csvan Avatar asked Aug 02 '15 00:08

csvan


People also ask

Can you cluster multiple node processors?

A cluster module executes the same Node. js process multiple times. Therefore, the first thing you need to do is to identify what portion of the code is for the master process and what portion is for the workers.

What is the difference between cluster and Worker_threads packages in node js?

Clusters of Node. js processes can be used to run multiple instances of Node. js that can distribute workloads among their application threads. When process isolation is not needed, use the worker_threads module instead, which allows running multiple application threads within a single Node.

What is the default port for node server?

The default port for HTTP is 80 – Generally, most web browsers listen to the default port. Below is the code implementation for creating a server in node and making it listen to port 80.


1 Answers

You can pass environment variables to each child, allowing the master process to assign them ports:

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

if (cluster.isMaster) {
  var pidToPort = {}; 
  var worker, port;
  for (var i = 0; i < numCPUs; i++) {
    port = 8000 + i;
    worker = cluster.fork({port: port});
    pidToPort[worker.process.pid] = port;
  }

  console.log(pidToPort);

  cluster.on('exit', function(worker, code, signal) {
    // Use `worker.process.pid` and `pidToPort` to spin up a new worker with
    // the port that's now missing.  If you do so, don't forget to delete the
    // old `pidToPort` mapping and add the new one.
    console.log('worker ' + worker.process.pid + ' died');
  }); 
} else {
  // Start listening on `process.env.port` - but first, remember that it has
  // been cast to a string, so you'll need to parse it.
  console.log(process.env.port);
}
like image 74
Aaron Dufour Avatar answered Oct 24 '22 14:10

Aaron Dufour