Skip to content
This repository has been archived by the owner on Mar 16, 2022. It is now read-only.

Can we have middleware to support custom load-balancing? #109

Closed
MrSpark2591 opened this issue May 15, 2017 · 3 comments
Closed

Can we have middleware to support custom load-balancing? #109

MrSpark2591 opened this issue May 15, 2017 · 3 comments

Comments

@MrSpark2591
Copy link

MrSpark2591 commented May 15, 2017

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.

@tomas-fp
Copy link

tomas-fp commented May 15, 2017

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()
})

@MrSpark2591
Copy link
Author

MrSpark2591 commented May 16, 2017

@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.

@tomas-fp
Copy link

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()
})

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants