-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbanlancer.roundRobin.go
85 lines (77 loc) · 1.82 KB
/
banlancer.roundRobin.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
77
78
79
80
81
82
83
84
85
package main
import "math"
type NodePartLoadBalance struct {
CurrentWeight int // 当前状态
Danger int // 表示节点可能不可用
DangerCount int // 略过加权次数(降权处罚)
Delay int // 延迟,根据延迟调整优先级
}
func (n *node) SetDelay(delay int) {
if delay < 0 {
return
}
n.lock.Lock()
n.Delay = delay
n.Danger = 0
n.DangerCount = 0
n.lock.Unlock()
}
func (n *node) rm() {
n.lock.Lock()
if n.NodePartLoadBalance.Danger == 0 {
n.NodePartLoadBalance.Danger = 1
n.NodePartLoadBalance.DangerCount = 1
n.NodePartLoadBalance.CurrentWeight = 0
} else {
if n.NodePartLoadBalance.Danger < math.MaxInt8 {
n.NodePartLoadBalance.Danger *= 2
}
n.NodePartLoadBalance.DangerCount = n.NodePartLoadBalance.Danger
n.NodePartLoadBalance.CurrentWeight = 0
}
n.lock.Unlock()
}
//平滑加权轮询策略
func loadBalance(m *serviceNodes) int64 {
m.lock.RLock()
defer m.lock.RUnlock()
totalWeight := 0
maxDelay := 0
maxWeight := -1
maxId := int64(0)
//遍历所有节点
//累计权重到totalWeight
//每个节点CurrentWeight根据延迟自增
for id, node := range m.nodes {
if node.Danger > 0 && node.DangerCount > 0 {
node.DangerCount--
} else {
var w int
if node.Delay < int(m.lastMaxDelay) {
w = int(m.lastMaxDelay) - node.Delay
} else {
w = 1
}
node.CurrentWeight += w
totalWeight += w
if node.Delay > maxDelay {
maxDelay = node.Delay
}
}
if node.CurrentWeight > maxWeight {
maxWeight = node.CurrentWeight
maxId = id
}
}
if maxId == 0 {
panic(1)
}
//CurrentWeight最大节点减去总权重
if m.nodes[maxId].CurrentWeight > totalWeight {
m.nodes[maxId].CurrentWeight -= totalWeight
} else {
m.nodes[maxId].CurrentWeight = 0
}
//节点列表中CurrentWeight最大的为本次选中节点
return maxId
}