Skip to content

Instantly share code, notes, and snippets.

@niftylettuce
Created December 2, 2012 20:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niftylettuce/411b0a2185b3ee4681b2 to your computer and use it in GitHub Desktop.
Save niftylettuce/411b0a2185b3ee4681b2 to your computer and use it in GitHub Desktop.
Node 0.8.x cluster module usage with @substack's bouncy module using CLI example for req.on('error').
// # cluster-with-bouncy
var cluster = require('cluster')
, numCPUS = require('os').cpus().length
, bouncy = require('bouncy')
, sites = require('./sites')
if (cluster.isMaster) {
// fork workers
for(var i=0; i < numCPUS; i+=1) {
cluster.fork()
}
// exit
cluster.on('exit', clusterExit)
} else {
// bouncy workers
bouncy(bounceWorker).listen(80)
}
function processMsg(msg) {
console.log('msg', msg)
}
function clusterExit(worker, code, signal) {
console.log('worker %s died', worker.process.pid)
}
function bounceWorker(req, bounce) {
req.on('error', bounceError)
var host = (req.headers.host || '').replace(/:\d+$/, '')
, route = sites[host] || 8080
bounce(route).on('error', bounceError)
function bounceError(err) {
var res = bounce.respond()
res.statusCode = 500
res.end('error\r\n')
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment