eye-gu commented on code in PR #4935:
URL: https://github.com/apache/shenyu/pull/4935#discussion_r1280060340


##########
shenyu-loadbalancer/src/main/java/org/apache/shenyu/loadbalancer/spi/RandomLoadBalancer.java:
##########
@@ -41,42 +42,41 @@ public Upstream doSelect(final List<Upstream> upstreamList, 
final String ip) {
         weights[0] = firstUpstreamWeight;
         // init the totalWeight
         int totalWeight = firstUpstreamWeight;
-        int halfLengthTotalWeight = 0;
         for (int i = 1; i < length; i++) {
             int currentUpstreamWeight = getWeight(upstreamList.get(i));
-            if (i <= (length + 1) / 2) {
-                halfLengthTotalWeight = totalWeight;
-            }
-            weights[i] = currentUpstreamWeight;
             totalWeight += currentUpstreamWeight;
+            weights[i] = totalWeight;
             if (sameWeight && currentUpstreamWeight != firstUpstreamWeight) {
                 // Calculate whether the weight of ownership is the same.
                 sameWeight = false;
             }
         }
         if (totalWeight > 0 && !sameWeight) {
-            return random(totalWeight, halfLengthTotalWeight, weights, 
upstreamList);
+            return random(totalWeight, weights, upstreamList, length);
         }
         return random(upstreamList);
     }
 
-    private Upstream random(final int totalWeight, final int 
halfLengthTotalWeight, final int[] weights, final List<Upstream> upstreamList) {
+    private Upstream random(final int totalWeight, final int[] weights, final 
List<Upstream> upstreamList, final int length) {
         // If the weights are not the same and the weights are greater than 0, 
then random by the total number of weights.
         int offset = RANDOM.nextInt(totalWeight);
-        int index = 0;
-        int end = weights.length;
-        if (offset >= halfLengthTotalWeight) {
-            index = (weights.length + 1) / 2;
-            offset -= halfLengthTotalWeight;
+        if (length <= 4) {

Review Comment:
   > when service lists are 5?
   
   Then it will be searched through binary search



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to