Skip to content

Commit

Permalink
Optimize the doSelect method of RandomLoadBalance to reduce the times…
Browse files Browse the repository at this point in the history
… of invoke of the getWeight method of the AbstractLoadBalance (#2597)
  • Loading branch information
tswstarplanet authored and kimmking committed Oct 21, 2018
1 parent c805c1d commit 5e60bef
Showing 1 changed file with 4 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ public class RandomLoadBalance extends AbstractLoadBalance {
@Override
protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation) {
int length = invokers.size(); // Number of invokers
int totalWeight = 0; // The sum of weights
boolean sameWeight = true; // Every invoker has the same weight?
for (int i = 0; i < length; i++) {
int firstWeight = getWeight(invokers.get(0), invocation);
int totalWeight = firstWeight; // The sum of weights
for (int i = 1; i < length; i++) {
int weight = getWeight(invokers.get(i), invocation);
totalWeight += weight; // Sum
if (sameWeight && i > 0
&& weight != getWeight(invokers.get(i - 1), invocation)) {
if (sameWeight && weight != firstWeight) {
sameWeight = false;
}
}
Expand Down

0 comments on commit 5e60bef

Please sign in to comment.