Reputation: 14178
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.
Upvotes: 1
Views: 222
Reputation: 203329
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);
Upvotes: 2