-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathservers.go
More file actions
64 lines (51 loc) · 1.23 KB
/
servers.go
File metadata and controls
64 lines (51 loc) · 1.23 KB
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
package balancer
import (
"math"
"sort"
)
// Servers - list of servers
type Servers []*Server
func (s Servers) eachASYNC(fn func(int, *Server)) Servers {
for i, n := range s {
go fn(i, n)
}
return s
}
func (s Servers) filterBySecondsBehindMaster() Servers {
minValue := math.MaxInt64
indexesByValue := make(map[int][]int)
for i := 0; i < len(s); i++ {
current := s[i].health.secondsBehindMaster
if current == nil {
continue
}
indexesByValue[*current] = append(indexesByValue[*current], i)
if *current < minValue {
minValue = *current
}
}
var filteredServers Servers
for i := range s {
for _, index := range indexesByValue[minValue] {
if i != index {
continue
}
filteredServers = append(filteredServers, s[i])
}
}
sort.Sort(bySecondsBehindMaster(filteredServers))
return filteredServers
}
func (s Servers) filterByWriteSetStatus() Servers {
var filteredServers Servers
for i := 0; i < len(s); i++ {
if !s[i].health.IORunning() || !s[i].health.GetWriteSetReady() {
continue
}
if state := s[i].health.GetWriteSetReplicationState(); state != nil && *state != WriteSetStateSync || state == nil {
continue
}
filteredServers = append(filteredServers, s[i])
}
return filteredServers
}