1

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.

2
  • 1
    Pretty 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? Commented 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. Commented Dec 15, 2013 at 23:37

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

3 Comments

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.
@Dave that's not strictly necessary (this answer explains why) :)
Thanks, that makes a lot more sense to me now!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.