This is an automated email from the ASF dual-hosted git repository.
laurence pushed a commit to branch 3.0
in repository https://gitbox.apache.org/repos/asf/dubbo-go.git
The following commit(s) were added to refs/heads/3.0 by this push:
new 01de5d587 feat: RandomLoadBalance code optimization, update that how
to judge the same weight
new f6405fa79 Merge pull request #1899 from
Codeprh/feture/loadBalancingAlgorithmUpdate
01de5d587 is described below
commit 01de5d587a17f1781de597b06e9edb97a20c8d48
Author: codingprh <[email protected]>
AuthorDate: Mon May 16 10:08:39 2022 +0800
feat: RandomLoadBalance code optimization, update that how to judge the
same weight
---
cluster/loadbalance/random/loadbalance.go | 23 +++++++++++++----------
1 file changed, 13 insertions(+), 10 deletions(-)
diff --git a/cluster/loadbalance/random/loadbalance.go
b/cluster/loadbalance/random/loadbalance.go
index ae05b2d19..ea9ee981a 100644
--- a/cluster/loadbalance/random/loadbalance.go
+++ b/cluster/loadbalance/random/loadbalance.go
@@ -42,23 +42,27 @@ func NewRandomLoadBalance() loadbalance.LoadBalance {
}
func (lb *randomLoadBalance) Select(invokers []protocol.Invoker, invocation
protocol.Invocation) protocol.Invoker {
+
+ // Number of invokers
var length int
if length = len(invokers); length == 1 {
return invokers[0]
}
+
+ // Every invoker has the same weight?
sameWeight := true
+ // the maxWeight of every invokers, the minWeight = 0 or the maxWeight
of the last invoker
weights := make([]int64, length)
+ // The sum of weights
+ var totalWeight int64 = 0
- firstWeight := loadbalance.GetWeight(invokers[0], invocation)
- totalWeight := firstWeight
- weights[0] = firstWeight
-
- for i := 1; i < length; i++ {
+ for i := 0; i < length; i++ {
weight := loadbalance.GetWeight(invokers[i], invocation)
- weights[i] = weight
-
+ //Sum
totalWeight += weight
- if sameWeight && weight != firstWeight {
+ // save for later use
+ weights[i] = totalWeight
+ if sameWeight && totalWeight != weight*(int64(i+1)) {
sameWeight = false
}
}
@@ -68,8 +72,7 @@ func (lb *randomLoadBalance) Select(invokers
[]protocol.Invoker, invocation prot
offset := rand.Int63n(totalWeight)
for i := 0; i < length; i++ {
- offset -= weights[i]
- if offset < 0 {
+ if offset < weights[i] {
return invokers[i]
}
}