All Downloads are FREE. Search and download functionalities are using the official Maven repository.

netflix.ocelli.selectors.WeightedSelectionStrategy Maven / Gradle / Ivy

There is a newer version: 0.1.0-rc.2
Show newest version
package netflix.ocelli.selectors;

import rx.Observable;
import rx.Observable.OnSubscribe;
import rx.Subscriber;
import rx.functions.Func1;

/**
 * Load balancer that selects Clients based on weight
 * 
 * @author elandau
 *
 * @param 
 */
public class WeightedSelectionStrategy implements Func1, Observable> {
    
    private final WeightSelector func;
    
    public WeightedSelectionStrategy(WeightSelector func) {
        this.func = func;
    }
    
    @Override
    public Observable call(final ClientsAndWeights hosts) {
        if (hosts.isEmpty()) {
            return Observable.empty();
        }
        
        return Observable.create(new OnSubscribe() {
            int pos = func.call(hosts.getWeights(), hosts.getTotalWeights());
            
            @Override
            public void call(Subscriber t1) {
                t1.onNext(hosts.getClients().get(pos++ % hosts.getClients().size()));
                t1.onCompleted();
            }
        });
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy