Skip to content

Instantly share code, notes, and snippets.

@collinschaafsma
Created May 11, 2012 18:55
Show Gist options
  • Save collinschaafsma/2661685 to your computer and use it in GitHub Desktop.
Save collinschaafsma/2661685 to your computer and use it in GitHub Desktop.
node cluster
// Fast
var http = require('http');
http.Server(function(req, res) {
res.writeHead(200);
res.end("hello world\n");
}).listen(8000);
// Faster
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);
}
// Both fall over though at about the same rate under lots of load :(
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment