io.reactivex.netty.examples.tcp.loadbalancing.AbstractLoadBalancer Maven / Gradle / Ivy
/*
* Copyright 2016 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package io.reactivex.netty.examples.tcp.loadbalancing;
import io.reactivex.netty.channel.Connection;
import io.reactivex.netty.client.ConnectionProvider;
import io.reactivex.netty.client.HostConnector;
import io.reactivex.netty.client.events.ClientEventListener;
import io.reactivex.netty.client.loadbalancer.HostHolder;
import io.reactivex.netty.client.loadbalancer.LoadBalancingStrategy;
import rx.Observable;
import rx.Observable.OnSubscribe;
import rx.functions.Func1;
import java.net.SocketException;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
public abstract class AbstractLoadBalancer implements LoadBalancingStrategy {
private final Func1 nextIndexFinder;
protected AbstractLoadBalancer(Func1 nextIndexFinder) {
this.nextIndexFinder = nextIndexFinder;
}
protected AbstractLoadBalancer() {
this(new Func1() {
private final AtomicInteger nextIndex = new AtomicInteger();
@Override
public Integer call(Integer maxValue) {
return nextIndex.incrementAndGet() % maxValue;
}
});
}
@Override
public ConnectionProvider newStrategy(final List> hosts) {
final int size = hosts.size();
return () -> Observable.create((OnSubscribe>) subscriber -> {
ConnectionProvider hostToUse;
HostHolder host1 = hosts.get(nextIndexFinder.call(size));
HostHolder host2 = hosts.get(nextIndexFinder.call(size));
long weight1 = getWeight(host1.getEventListener());
long weight2 = getWeight(host2.getEventListener());
if (weight1 >= weight2) {
hostToUse = host1.getConnector().getConnectionProvider();
} else {
hostToUse = host2.getConnector().getConnectionProvider();
}
hostToUse.newConnectionRequest().unsafeSubscribe(subscriber);
}).retry((count, th) -> count < 3 && th instanceof SocketException);
}
@Override
public final HostHolder toHolder(HostConnector connector) {
return new HostHolder<>(connector, newListener());
}
protected abstract ClientEventListener newListener();
protected abstract long getWeight(ClientEventListener eventListener);
}