-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathbalancer.go
178 lines (144 loc) · 3.6 KB
/
balancer.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
174
175
176
177
178
package main
import (
"sort"
"sync/atomic"
)
// Balancing functions return a slice of all known available backends, in
// priority order. This way the service can cycle through backends if the
// initial connections fails.
// RR is always weighted.
// we don't reduce the weight, we just distribute exactly "Weight" calls in
// a row
func (s *Service) roundRobin() []*Backend {
s.Lock()
defer s.Unlock()
count := len(s.Backends)
switch count {
case 0:
return nil
case 1:
// fast track for the single backend case
return s.Backends[0:1]
}
// we may be out of range if we lost a backend since last connections
if s.lastBackend >= count {
s.lastBackend = 0
s.lastCount = 0
}
// if our backend was over-weight, but we can't find another, use this
var reuse *Backend
var balanced []*Backend
// Find the next Up backend to call
for i := 0; i < count; i++ {
backend := s.Backends[s.lastBackend]
if backend.Up() {
if s.lastCount >= int(backend.Weight) {
// used too many times, but save it just in case
reuse = backend
s.lastBackend = (s.lastBackend + 1) % count
s.lastCount = 0
continue
}
s.lastCount++
balanced = append(balanced, backend)
break
}
s.lastBackend = (s.lastBackend + 1) % count
}
if len(balanced) == 0 {
if reuse != nil {
balanced = append(balanced, reuse)
} else {
return nil
}
}
// Now add the rest of the available backends in order, in case the first
// connect fails
lastBackend := s.lastBackend
for i := 0; i < count-1; i++ {
lastBackend = (lastBackend + 1) % count
backend := s.Backends[lastBackend]
if backend.Up() {
balanced = append(balanced, backend)
}
}
return balanced
}
// LC returns the backend with the least number of active connections
func (s *Service) leastConn() []*Backend {
s.Lock()
defer s.Unlock()
count := len(s.Backends)
switch count {
case 0:
return nil
case 1:
// fast track for the single backend case
return s.Backends[0:1]
}
// return the backends in the order of least connections
var balanced []*Backend
// Accumulate all backends that are currently Up
for _, b := range s.Backends {
if b.Up() {
balanced = append(balanced, b)
}
}
if len(balanced) == 0 {
return nil
}
sort.Sort(ByActive(balanced))
return balanced
}
// Simple, but still weighted, RR for UDP where we don't don't have active
// connections or connection failures.
func (s *Service) udpRoundRobin() *Backend {
s.Lock()
defer s.Unlock()
count := len(s.Backends)
switch count {
case 0:
return nil
case 1:
// fast track for the single backend case
return s.Backends[0]
}
// we may be out of range if we lost a backend since last connections
if s.lastBackend >= count {
s.lastBackend = 0
s.lastCount = 0
}
// if our backend was over-weight, but we can't find another, use this
var backend, reuse *Backend
// Find the next Up backend to call
for i := 0; i < count; i++ {
backend = s.Backends[s.lastBackend]
if backend.Up() {
if s.lastCount >= int(backend.Weight) {
// used too many times, but save it just in case
reuse = backend
s.lastBackend = (s.lastBackend + 1) % count
s.lastCount = 0
continue
}
s.lastCount++
break
}
s.lastBackend = (s.lastBackend + 1) % count
}
if backend != nil {
return backend
}
if reuse != nil {
return reuse
}
return nil
}
type ByActive []*Backend
func (s ByActive) Len() int { return len(s) }
func (s ByActive) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s ByActive) Less(i, j int) bool {
iActive := atomic.LoadInt64(&(s[i].Active))
jActive := atomic.LoadInt64(&(s[j].Active))
return iActive < jActive
}