-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathswrr.go
173 lines (144 loc) · 2.56 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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package balancer
import (
"sync"
)
// Smooth weighted round-robin balancing
// Ref: https://github.com/phusion/nginx/commit/27e94984486058d73157038f7950a0a36ecc6e35
type swrr struct {
items []*swrrItem
count int
all map[string]int
sync.Mutex
}
type swrrItem struct {
item string
weight int
currentWeight int
}
func NewSmoothWeightedRoundRobin(items ...map[string]int) (lb *swrr) {
if len(items) > 0 && len(items[0]) > 0 {
lb = &swrr{}
lb.Update(items[0])
return
}
return &swrr{
all: make(map[string]int),
}
}
func (b *swrr) Add(item string, weight ...int) {
w := 1
if len(weight) > 0 {
w = weight[0]
}
b.Lock()
b.add(item, w)
b.Unlock()
}
func (b *swrr) add(item string, weight int) {
b.remove(item)
b.items = append(b.items, &swrrItem{
item: item,
weight: weight,
})
b.count++
b.all[item] = weight
}
func (b *swrr) All() interface{} {
all := make(map[string]int)
b.Lock()
for k, v := range b.all {
all[k] = v
}
b.Unlock()
return all
}
func (b *swrr) Name() string {
return "SmoothWeightedRoundRobin"
}
func (b *swrr) Select(_ ...string) (item string) {
b.Lock()
switch b.count {
case 0:
item = ""
case 1:
if b.items[0].weight > 0 {
item = b.items[0].item
}
default:
item = b.chooseNext().item
}
b.Unlock()
return
}
func (b *swrr) chooseNext() (choice *swrrItem) {
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) Remove(item string, _ ...bool) bool {
b.Lock()
defer b.Unlock()
return b.remove(item)
}
func (b *swrr) remove(item string) (ok bool) {
for i := 0; i < b.count; i++ {
if item == b.items[i].item {
b.items = append(b.items[:i], b.items[i+1:]...)
b.count--
delete(b.all, item)
ok = true
return
}
}
return
}
func (b *swrr) RemoveAll() {
b.Lock()
b.items = b.items[:0]
b.count = 0
b.all = make(map[string]int)
b.Unlock()
}
func (b *swrr) Reset() {
b.Lock()
for i := range b.items {
b.items[i].currentWeight = b.items[i].weight
}
b.Unlock()
}
func (b *swrr) Update(items interface{}) bool {
v, ok := items.(map[string]int)
if !ok {
return false
}
count := len(v)
data := make([]*swrrItem, count)
i := 0
for item, weight := range v {
data[i] = &swrrItem{
item: item,
weight: weight,
}
i++
}
b.Lock()
b.count = count
b.all = v
b.items = data
b.Unlock()
return true
}