h2non/rocky

Can we have middleware to support custom load-balancing?

MrSpark2591 opened this issue · 3 comments

Currently only .balance([server array]) takes care of load balancing and it's Round robin way of load balancing. What if I want to divide request load to server let's say 70% and 30% (assuming only two instances are running) depending on the health of the server.

So the feature I want is to have .balanceFunction() and where we can add custom code for load balancing.

Thank you.

Yes, you can write your custom middleware that does this for you.

You can simply use something like this:

proxy.use(function myCustomBalancer (req, res, next) {
  const urls = ['http://server1', 'http://server2']
  const target = urls[Math.random() > 0.3 ? 0 : 1]
  req.rocky.options.target = target
  next()
})

@tomas-fp Thank you so much :)
So this will actually work for global routes. What if i want to have different routes for different services. let's say
proxy.all('/service1') .balance([servers for service1])
and
proxy.all('/service2') .balance([servers for service2])
In this case, it will not work. I found work around to change target option in .useForward(). but anyhow RR(default load balancing) will be executed before that to choose target server. So will it be performance issue in long term or can you suggest any other efficient solution in this case.

Just do the same at route level (which also implements a middleware extensible interface):

proxy.all('/service1').use(function myCustomBalancer (req, res, next) {
  const urls = ['http://server1', 'http://server2']
  const target = urls[Math.random() > 0.3 ? 0 : 1]
  req.rocky.options.target = target
  next()
})