h2non / rocky

Full-featured, middleware-oriented, programmatic HTTP and WebSocket proxy for node.js (deprecated)
MIT License
371 stars 24 forks source link

How to update load balance ? #103

Closed MrSpark2591 closed 7 years ago

MrSpark2591 commented 7 years ago

I am trying to update load balance for this code. here i have loadBalancing map which is something like : loadBalancingMap = { serviceName : [url1,url2] } and i am using it o register service and balancer like this : proxy .all('/'.concat(serviceName).concat('/*')) .balance(loadBalancingMap[serviceName])

now how can i update balance for this service when there is one more instance of service or some instance failed.

h2non commented 7 years ago

You can simply call .balance() method again.

MrSpark2591 commented 7 years ago

@h2non yes, I thought that and tried. this is what I have done.here what I am doing is reassigning the whole thing by calling this function whenever my 'loadBalancingMap' value change. can you tell me what I am doing wrong? I know it's just silly thing but I am really not getting how to overcome.


var regeisterProxyRoute = function(serviceName){
     proxy
    .all('/'.concat(serviceName).concat('/*'))
    .balance(loadBalancingMap[serviceName])
};
tomas-fp commented 7 years ago

You have to store the route object reference you're implicitly creating via: proxy.all('/'.concat(serviceName).concat('/*')).

So your code might look like:

var routes = {}

var registerProxyRoute = function (serviceName) {
    const path = '/'.concat(serviceName).concat('/*')
    const route = routes[path] = routes[path] || proxy.all(path)    
    route.balance(loadBalancingMap[serviceName])
}