1.8 KiB
Cluster
A single instance of Node runs in a single thread. To take advantage of multi-core systems the user will sometimes want to launch a cluster of Node processes to handle the load.
The cluster module allows you to easily create a network of processes all which share server ports.
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('death', function(worker) {
console.log('worker ' + worker.pid + ' died');
});
} else {
// Worker processes have a http server.
http.Server(function(req, res) {
res.writeHead(200);
res.end("hello world\n");
}).listen(8000);
}
Running node will now share port 8000 between the workers:
% node server.js
Worker 2438 online
Worker 2437 online
cluster.fork()
Spawn a new worker process. This can only be called from the master process.
cluster.isMaster
cluster.isWorker
Boolean flags to determine if the current process is a master or a worker
process in a cluster. A process isMaster
if process.env.NODE_WORKER_ID
is undefined.
cluster.eachWorker(cb)
Synchronously iterates over all of the workers.
cluster.eachWorker(function(worker) {
console.log("worker pid=" + worker.pid);
});
cluster.workerCount()
Returns the number of workers.
Event: 'death'
When any of the workers die the cluster module will emit the 'death' event.
This can be used to restart the worker by calling fork()
again.
cluster.on('death', function(worker) {
console.log('worker ' + worker.pid + ' died. restart...');
cluster.fork();
});
Different techniques can be used to restart the worker depending on the application.