-
-
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').
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
// # 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