Node.js Cluster
master.js
var http = require('http'),
numCPUs = require('os').cpus().length;
cp = require('child_process'),
net = require('net');
var workers = [];
for (var i = 0; i < numCPUs; i++) {
workers.push(cp.fork('app.js', ['normal']));
}
net.createServer(function(s) {
s.pause();
var worker = worker.shift();
worker.send('c',s);
workers.push(worker);
}).listen(80);
worker.js
var http = require('http'),
cp = require('child_process'),
net = require('net');
var server = http.createServer(function(req,res){
res.writeHead(200, {"Content-Type": "text/plain", "Connection": "close"});
res.end("hello, world");
});
console.log("webServer started on " + process.pid);
process.on("message", function(msg,socket) {
process.nextTick(function(){
if(msg == 'c' && socket) {
socket.readable = socket.writable = true;
socket.resume();
server.connections++;
socket.server = server;
server.emit("connection", socket);
socket.emit("connect");
}
});
});