Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.