-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathweight.go
82 lines (67 loc) · 1.82 KB
/
weight.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
package weight
// this package implement weighted round-robin load balancing algorithm
// ---
// first, it will increase all backend current_weight by their respective weight
// then it will choose backend server that have biggest current_weight
// after that, current_weight of selected backend will be reduced by total_weight
// ~ repeat.
// Backend hold information about backend server
type Backend struct {
Name string
Weight int
CurrentWeight int
}
// Weight list of all backend server
type Weight struct {
Items []*Backend
}
// NewWeight returns Weight struct
func NewWeight() *Weight {
return &Weight{}
}
// AddWeight add server and weight information
func (w *Weight) AddWeight(name string, weight int) {
w.Items = append(w.Items, &Backend{
Name: name,
Weight: weight,
})
}
// Next returns next selected server
func (w *Weight) Next() string {
// update current_weight by adding it with weight
for i := 0; i < len(w.Items); i++ {
w.Items[i].CurrentWeight += w.Items[i].Weight
}
return w.nextSelectedServer()
}
// nextSelectedServer returns selected server and update current_weight
func (w *Weight) nextSelectedServer() string {
var server string
var n, index int
for i, b := range w.Items {
if n < b.CurrentWeight {
n = b.CurrentWeight
server = b.Name
index = i
}
}
w.updateEffectiveWeight(index)
return server
}
// updateEffectiveWeight update current_weight by reducing it with total_weight
// current_weight - total_weight
func (w *Weight) updateEffectiveWeight(index int) {
for i := 0; i < len(w.Items); i++ {
if i == index {
w.Items[i].CurrentWeight -= w.getTotalWeight()
}
}
}
// getTotalWeight get total weight of all weighted server
func (w *Weight) getTotalWeight() int {
totalWeight := 0
for _, t := range w.Items {
totalWeight += t.Weight
}
return totalWeight
}