Skip to content

Instantly share code, notes, and snippets.

@aseemk
Created January 9, 2013 21:58
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aseemk/4497350 to your computer and use it in GitHub Desktop.
Save aseemk/4497350 to your computer and use it in GitHub Desktop.
Node.js cluster support on Heroku — freaking sweet!
var cluster = require('cluster');
var http = require('http');
var numCPUs = require('os').cpus().length;
var pid = process.pid;
var port = process.env['PORT'] || 8000;
if (cluster.isMaster) {
console.log('Master:', pid);
// Fork workers -- one less than the number of CPUs.
for (var i = 1; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('death', function(worker) {
console.error('DEATH:', worker.pid);
});
} else {
console.log('Worker:', pid);
// Worker processes have a http server.
http.createServer(function(req, res) {
console.log('Request:', pid);
res.writeHead(200);
res.end("Hello world! (From process " + pid + ".)\n");
}).listen(port);
}
@aseemk
Copy link
Author

aseemk commented Sep 25, 2013

I'd recommend using https://github.com/brianc/node-forky now instead of this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment