-
Notifications
You must be signed in to change notification settings - Fork 0
/
WeightedPollingLoad.go
76 lines (65 loc) · 1.76 KB
/
WeightedPollingLoad.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package load_balance
import (
"errors"
"strconv"
)
type WeightRoundRobinBalance struct {
curIndex int
rss []*WeightNode
rsw []int
}
type WeightNode struct {
addr string
Weight int //初始化时对节点约定的权重
currentWeight int //节点临时权重,每轮都会变化
effectiveWeight int //有效权重, 默认与weight相同 , totalWeight = sum(effectiveWeight) //出现故障就-1
}
//1, currentWeight = currentWeight + effectiveWeight
//2, 选中最大的currentWeight节点为选中节点
//3, currentWeight = currentWeight - totalWeight
func (r *WeightRoundRobinBalance) Add(params ...string) error {
if len(params) != 2 {
return errors.New("params len need 2")
}
parInt, err := strconv.ParseInt(params[1], 10, 64)
if err != nil {
return err
}
node := &WeightNode{
addr: params[0],
Weight: int(parInt),
}
node.effectiveWeight = node.Weight
r.rss = append(r.rss, node)
return nil
}
func (r *WeightRoundRobinBalance) Next() string {
var best *WeightNode
total := 0
for i := 0; i < len(r.rss); i++ {
w := r.rss[i]
//1 计算所有有效权重
total += w.effectiveWeight
//2 修改当前节点临时权重
w.currentWeight += w.effectiveWeight
//3 有效权重默认与权重相同,通讯异常时-1, 通讯成功+1,直到恢复到weight大小
if w.effectiveWeight < w.Weight {
w.effectiveWeight++
}
//4 选中最大临时权重节点
if best == nil || w.currentWeight > best.currentWeight {
best = w
}
}
if best == nil {
return ""
}
//5 变更临时权重为 临时权重-有效权重之和
best.currentWeight -= total
return best.addr
}
func (r *WeightRoundRobinBalance) Get(string) (string, error) {
return r.Next(), nil
}
func (r *WeightRoundRobinBalance) Update() {
}