I'd like to use node http-proxy to create a "least connections" proxy. In other words it picks a backend that currently has the least connections. The proxy has an "end" event, but it doesn't pass you any information, so I'm not sure how I can increment/decrement a counter for each of my backends with the current number of concurrent requests.
-
1Pretty sure there's nothing out there that can do it -- you'll probably have to build it yourself using websockets. Alternatively, have you looked at satellite?brandonscript– brandonscript2013-12-15 06:44:33 +00:00Commented Dec 15, 2013 at 6:44
-
Thanks for the pointer to satellite. Ultimately, I'm also going to have this manage processes on the local host (could be java, node, etc ... ) and have some other features around recycling them, etc ... so the satellite code will be good to look at.Dave– Dave2013-12-15 23:37:55 +00:00Commented Dec 15, 2013 at 23:37
Add a comment
|
1 Answer
I think you can wait for the response to have been send to the client.
For example:
var backends = [
{ host : 'backend1', port: 80, active : 0 },
{ host : 'backend2', port: 80, active : 0 },
{ host : 'backend3', port: 80, active : 0 },
{ host : 'backend4', port: 80, active : 0 },
];
httpProxy.createServer(function (req, res, proxy) {
var buffer = httpProxy.buffer(req);
// Pick the backend with the least active requests (naive implementation).
var backend = backends.sort(function(a, b) {
return a.active - b.active;
})[0];
// Add a new active request.
backend.active++;
// Proxy the request.
proxy.proxyRequest(req, res, {
host : backend.host,
port : backend.port,
buffer : buffer
});
// When the response is finished, decrease the count.
res.on('finish', function() {
backend.active--;
});
}).listen(8000);
3 Comments
Dave
Thanks! I'll probably setup the res.on('finish') before I proxy.proxyRequest() just in case the res finishes before I get to setting up the callback.
robertklep
@Dave that's not strictly necessary (this answer explains why) :)
Dave
Thanks, that makes a lot more sense to me now!