Skip to content

Commit

Permalink
repair Divisor equals zero #Bug
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuCheer committed Sep 26, 2019
1 parent e8c6870 commit 541f72e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
9 changes: 8 additions & 1 deletion balancer/wroundrobin.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ func (r *WRoundRobinLoad) reloadActiveItems() error {
for _, item := range target.Items {
originItems = append(originItems, item)
}

gcdWeight, err := getGCDWeight(originItems)
if err != nil {
return err
Expand Down Expand Up @@ -149,7 +148,15 @@ func getGCDWeight(items []OriginItem) (uint32, error) {
if len(items) == 0 {
return 0, errors.New("origin items is empty")
}
for k, val := range items {
if val.Weight == 0 {
items[k].Weight = 1
}
}

if len(items) < 2 {
return items[0].Weight, nil
}
var gcdWeight uint32
for i := 1; i < len(items); i++ {
if i == 1 {
Expand Down
9 changes: 8 additions & 1 deletion balancer/wroundrobin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,21 @@ func TestGetGCDWeight(t *testing.T) {
if err == nil {
t.Error("getGCDWeight func have an error #1")
}
items = []OriginItem{
{"127.0.0.1:8081", 0},
}
gcdWeight, _ := getGCDWeight(items)
if gcdWeight != 1 {
t.Error("getGCDWeight func have an error #1.1")
}

items = []OriginItem{
{"192.168.137.100", 80},
{"192.168.137.100", 130},
{"192.168.137.100", 40},
{"192.168.137.100", 20},
}
gcdWeight, _ := getGCDWeight(items)
gcdWeight, _ = getGCDWeight(items)
if gcdWeight != 10 {
t.Error("getGCDWeight func have an error #2")
}
Expand Down

0 comments on commit 541f72e

Please sign in to comment.