rpc.turbo.loadbalance.RoundRobinLoadBalance Maven / Gradle / Ivy
The newest version!
package rpc.turbo.loadbalance;
import java.util.List;
import rpc.turbo.util.concurrent.ConcurrentIntegerSequencer;
/**
* 速度最快 204.642 ± 1.959 ops/us
*
* @author Hank
*
* @param
* 必须为Weightable子类
*/
public class RoundRobinLoadBalance implements LoadBalance {
private final ConcurrentIntegerSequencer sequencer = new ConcurrentIntegerSequencer(0, true);
protected volatile WeightableGroup weightableGroup = null;
@Override
public void setWeightables(List weightables) {
weightableGroup = new WeightableGroup<>(weightables);
}
@Override
public T select() {
final WeightableGroup weightableGroup = this.weightableGroup;
if (weightableGroup == null) {
return null;
}
int sum = weightableGroup.sum();
if (sum < 2) {
return weightableGroup.get(0);
}
int seed = sequencer.next();
return weightableGroup.get(seed);
}
}