-
Notifications
You must be signed in to change notification settings - Fork 1
/
swrr.go
60 lines (49 loc) · 1.01 KB
/
swrr.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
package balancer
// Smooth weighted round-robin balancing
// Ref: https://github.com/phusion/nginx/commit/27e94984486058d73157038f7950a0a36ecc6e35
type swrr struct {
items []*Choice
count int
}
func NewSmoothWeightedRoundRobin(choices ...*Choice) (lb *swrr) {
lb = &swrr{}
lb.Update(choices)
return
}
func (b *swrr) Select(_ ...string) (item interface{}) {
switch b.count {
case 0:
item = nil
case 1:
item = b.items[0].Item
default:
item = b.chooseNext().Item
}
return
}
func (b *swrr) chooseNext() (choice *Choice) {
total := 0
for i := range b.items {
c := b.items[i]
if c == nil {
return nil
}
total += c.Weight
c.CurrentWeight += c.Weight
if choice == nil || c.CurrentWeight > choice.CurrentWeight {
choice = c
}
}
if choice == nil {
return nil
}
choice.CurrentWeight -= total
return choice
}
func (b *swrr) Name() string {
return "SmoothWeightedRoundRobin"
}
func (b *swrr) Update(choices []*Choice) bool {
b.items, b.count = cleanWeight(choices)
return b.count > 0
}