-
Notifications
You must be signed in to change notification settings - Fork 1
/
cluster.go
164 lines (137 loc) · 2.86 KB
/
cluster.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
package clickhouse
import (
"math/rand"
"sync"
"time"
)
type pingStats struct {
mx sync.Mutex
count int
errors int
last int64
avg int64
}
func (p *pingStats) NewCheck(last int64, err bool) {
p.mx.Lock()
defer p.mx.Unlock()
if err {
p.errors++
return
}
p.last = last
if p.count == 0 {
p.avg = last
} else {
p.avg = int64((float64(p.avg)*float64(p.count) + float64(last)) / (float64(p.count) + 1))
}
p.count++
}
func (p *pingStats) Avg() int64 {
p.mx.Lock()
defer p.mx.Unlock()
return p.avg
}
// PingErrorFunc callback function, call whenever ping failed
type PingErrorFunc func(*Conn)
// Cluster is useful if you have several DBs with distributed or partitional logic. In this case you can send requests to random server to load balance and improve stability.
type Cluster struct {
conn map[*Conn]*pingStats
mx sync.Mutex
active []*Conn
onFail PingErrorFunc
onDown func()
}
// NewCluster create cluster from connections
func NewCluster(conn ...*Conn) *Cluster {
conns := make(map[*Conn]*pingStats)
for i := range conn {
conns[conn[i]] = &pingStats{}
}
return &Cluster{
conn: conns,
}
}
// IsDown check if there at least one working connection
func (c *Cluster) IsDown() bool {
c.mx.Lock()
defer c.mx.Unlock()
return len(c.active) < 1
}
// OnClusterDown callback func on all cluster is down
func (c *Cluster) OnClusterDown(f func()) {
c.onDown = f
}
// OnCheckError callback func on each fail ping
func (c *Cluster) OnCheckError(f PingErrorFunc) {
c.onFail = f
}
// ActiveConn return random active connection
func (c *Cluster) ActiveConn() *Conn {
c.mx.Lock()
defer c.mx.Unlock()
l := len(c.active)
if l < 1 {
return nil
}
return c.active[rand.Intn(l)]
}
// RankConn return list of connections with avg speed, include also not working connections (if some last pings failed)
func (c *Cluster) RankConn() map[*Conn]int64 {
rt := make(map[*Conn]int64)
for k, v := range c.conn {
rt[k] = v.Avg()
}
return rt
}
// BestConn return fastest connection
func (c *Cluster) BestConn() *Conn {
c.mx.Lock()
defer c.mx.Unlock()
l := len(c.active)
if l < 1 {
return nil
}
if l == 1 {
return c.active[0]
}
maxV := c.conn[c.active[0]].Avg()
maxK := c.active[0]
for i := range c.active {
tmp := c.conn[c.active[i]].Avg()
if tmp < maxV {
maxV = tmp
maxK = c.active[i]
}
}
return maxK
}
// Check call Ping for all connections and save active
func (c *Cluster) Check() {
var (
err error
res []*Conn
)
for conn, val := range c.conn {
// measure ping time
start := time.Now()
err = conn.Ping()
elapsed := time.Since(start)
val.NewCheck(elapsed.Nanoseconds(), err != nil)
if err == nil {
res = append(res, conn)
} else {
if c.onFail != nil {
c.onFail(conn)
}
}
}
if len(res) == 0 {
if c.onDown != nil {
c.onDown()
}
return
}
c.mx.Lock()
defer c.mx.Unlock()
c.active = res
}