Created
January 9, 2013 21:58
-
-
Save aseemk/4497350 to your computer and use it in GitHub Desktop.
Node.js cluster support on Heroku — freaking sweet!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'd recommend using https://github.com/brianc/node-forky now instead of this.