Created
September 29, 2011 07:45
-
-
Save ronkorving/1250211 to your computer and use it in GitHub Desktop.
Combining cluster and node-http-proxy for part master and part worker http responses
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'), | |
http = require('http'), | |
httpProxy = require('http-proxy'); | |
var httpPort = 4332; | |
var clusterSock = './cluster.sock'; | |
var httpServer = http.createServer(function (req, res) { | |
res.end('Worker! ' + process.pid); | |
}); | |
var cl = cluster(httpServer) | |
.set('workers', 5) | |
.listen(clusterSock); | |
if (cl.isMaster) { | |
var options = { | |
target: { host: 'localhost', port: clusterSock } | |
}; | |
httpProxy.createServer(options, function (req, res, proxy) { | |
if (Date.now() % 10 < 5) { | |
console.log('Relaying to worker'); | |
proxy.proxyRequest(req, res); | |
} else { | |
console.log('Master response'); | |
res.end('Master! ' + process.pid); | |
} | |
}).listen(httpPort); | |
console.log('HTTP proxy listening on port', httpPort); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment