-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbalancer.go
196 lines (162 loc) · 4.3 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package balancer
import (
"sort"
"sync/atomic"
"time"
"github.com/StudioSol/balancer/concurrence"
)
type bySecondsBehindMaster Servers
func (a bySecondsBehindMaster) Len() int { return len(a) }
func (a bySecondsBehindMaster) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a bySecondsBehindMaster) Less(i, j int) bool {
if a[i].health.secondsBehindMaster == nil && a[j].health.secondsBehindMaster == nil {
return false
}
if a[i].health.secondsBehindMaster == nil && a[j].health.secondsBehindMaster != nil {
return false
}
if a[i].health.secondsBehindMaster != nil && a[j].health.secondsBehindMaster == nil {
return true
}
return *a[i].health.secondsBehindMaster < *a[j].health.secondsBehindMaster
}
type byConnections Servers
func (a byConnections) Len() int { return len(a) }
func (a byConnections) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a byConnections) Less(i, j int) bool {
if a[i].health.runningConnections == nil && a[j].health.runningConnections != nil {
return false
}
if a[i].health.runningConnections != nil && a[j].health.runningConnections == nil {
return true
}
if a[i].health.runningConnections == nil || a[j].health.runningConnections == nil ||
*a[i].health.runningConnections == *a[j].health.runningConnections {
if a[i].health.openConnections == nil && a[j].health.openConnections == nil {
return false
}
if a[i].health.openConnections == nil && a[j].health.openConnections != nil {
return false
}
if a[i].health.openConnections != nil && a[j].health.openConnections == nil {
return true
}
return *a[i].health.openConnections < *a[j].health.openConnections
}
return *a[i].health.runningConnections < *a[j].health.runningConnections
}
// Balancer MySQL load balancer
type Balancer struct {
config *Config
servers Servers
logger Logger
traceOn bool
}
// GetServers ...
func (b *Balancer) GetServers() Servers {
return b.servers
}
// serversUP returns a slice of UP servers
func (b *Balancer) serversUP() Servers {
serversUP := make(Servers, 0, len(b.servers))
for _, server := range b.servers {
if server.health.IsUP() {
serversUP = append(serversUP, server)
}
}
return serversUP
}
func (b *Balancer) check() {
b.servers.eachASYNC(func(index int, server *Server) {
server.CheckHealth(b.traceOn, b.logger)
})
}
func (b *Balancer) waitCheck() {
wait := b.config.StartupWait
// default to 5s
if wait <= 0 {
wait = time.Second * 5
}
signal := make(chan struct{}, 0)
var expired atomic.Value
t := time.AfterFunc(wait, func() {
expired.Store(struct{}{})
signal <- struct{}{}
})
go func() {
for i := range b.servers {
if expired.Load() != nil {
return
}
b.servers[i].CheckHealth(b.traceOn, b.logger)
}
if t.Stop() {
signal <- struct{}{}
}
}()
<-signal
}
// PickServer returns the best server at a given point in time
func (b *Balancer) PickServer() *Server {
candidates := b.serversUP()
switch len(candidates) {
case 0:
return nil
case 1:
return candidates[0]
}
if b.config.ReplicationMode == ReplicationModeMultiSourceWriteSet {
candidates = candidates.filterByWriteSetStatus()
} else {
candidates = candidates.filterBySecondsBehindMaster()
}
switch len(candidates) {
case 0:
candidates = b.serversUP()
case 1:
return candidates[0]
}
if len(candidates) == 0 {
return nil
}
sort.Sort(byConnections(candidates))
return candidates[0]
}
// New creates a new instance of Balancer
func New(config *Config) *Balancer {
// Minimum check interval
if config.CheckInterval == 0 {
config.CheckInterval = 3
}
servers := make(Servers, len(config.ServersSettings))
for i, serverSettings := range config.ServersSettings {
servers[i] = &Server{
name: serverSettings.Name,
serverSettings: serverSettings,
health: &ServerHealth{
lastUpdate: time.Now(),
},
replicationMode: config.ReplicationMode,
}
}
balancer := &Balancer{
config: config,
servers: servers,
logger: config.Logger,
traceOn: config.TraceOn,
}
balancer.waitCheck()
if config.StartCheck {
concurrence.Every(time.Duration(config.CheckInterval)*time.Second, func(time.Time) bool {
balancer.check()
return true
})
}
return balancer
}
// Logger ...
type Logger interface {
Error(args ...interface{})
Errorf(format string, args ...interface{})
Printf(format string, v ...interface{})
}