/
pool.go
236 lines (196 loc) · 4.48 KB
/
pool.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package redis
import (
"errors"
"fmt"
"net"
"sync"
"time"
"github.com/gomodule/redigo/redis"
)
var (
MaxFail = 6
FailureRetry = 3
ServerEmptyErr = errors.New("redis: servers is empty")
ServerNonAvailableErr = errors.New("redis: server non available ")
WrongAnswer = errors.New("redis: get wrong answer")
EmptyAnswer = errors.New("redis: get empty answer")
)
type Server struct {
Host string
Port string
Auth string
DB string
}
type Pool struct {
mu *sync.RWMutex
// the redis instance index of pools
index int
pools []*redis.Pool
pool *redis.Pool
status map[int]bool
// 熵值策略可以防止抖动引起的主备切换,只有连接失败才进行主备切换
// 采用熵值[0,MaxFail]记录redis稳定性
// 连接失败熵值加一加到MaxFail进行切换,连接成功熵值减一减到0为止
entropy map[int]int
servers map[int]string
}
func (p *Pool) checkDoTest() bool {
p.mu.RLock()
defer p.mu.RUnlock()
for _, v := range p.status {
if v {
return false
}
}
return true
}
func (p *Pool) testAll() {
// 当所有的redis都不可用时,尝试重新探测一个可用的
if p.checkDoTest() {
for i := range p.pools {
go func(index int) {
conn := p.pools[index].Get()
_, err := conn.Do("PING")
var opError *net.OpError
if !errors.As(err, &opError) && p.checkDoTest() {
p.mu.Lock()
// switch redis instance to index
p.switchIndex(index)
p.mu.Unlock()
}
}(i)
}
}
}
// switchIndex switch redis instance to index.
func (p *Pool) switchIndex(index int) {
// NOTE: 调用前记得加锁
if index >= len(p.pools) {
return
}
// redis switch to index p.servers[index]
p.index = index
p.entropy[index] = 0
p.status[index] = true
p.pool = p.pools[index]
}
func (p *Pool) FailOver() {
// 当只有一个server连接时,不进行切换
if len(p.pools) == 1 {
return
}
// 记录失败连接数,当大于最大失败后切换server
p.mu.Lock()
defer p.mu.Unlock()
p.entropy[p.index] = p.entropy[p.index] + 1
if p.entropy[p.index] >= MaxFail {
p.status[p.index] = false
index := p.index + 1
if index >= len(p.pools) {
index = 0
}
p.switchIndex(index)
}
return
}
func (p *Pool) Recover() {
p.mu.Lock()
defer p.mu.Unlock()
if p.entropy[p.index] > 0 {
p.entropy[p.index] = p.entropy[p.index] - 1
}
return
}
func (p *Pool) Do(cmdStr string, args ...any) (reply any, err error) {
// actually do the redis commands
// 所有server状态异常后,每次都尝试重新测试
if p.checkDoTest() {
go p.testAll()
return nil, ServerNonAvailableErr
}
// 失败重试 FailureRetry 次
var conn redis.Conn
defer conn.Close()
for i := 0; i < FailureRetry; i++ {
conn = p.pool.Get()
if reply, err = conn.Do(cmdStr, args...); err != nil {
var opErr *net.OpError
if errors.As(err, &opErr) {
p.FailOver()
go p.testAll()
}
time.Sleep(100 * time.Millisecond)
continue
}
// 重试 i 次后成功
p.Recover()
break
}
return
}
func (p *Pool) Close() error {
for i := range p.pools {
p.pools[i].Close()
}
p.pool = nil
return nil
}
func NewRedisPool(s []Server) (*Pool, error) {
if len(s) <= 0 {
return nil, ServerEmptyErr
}
p := &Pool{
status: make(map[int]bool),
entropy: make(map[int]int),
servers: make(map[int]string),
}
for i := range s {
rp, _ := newTimeoutPool(s[i], 1*time.Second, 2*time.Second, 2*time.Second)
if _, err := rp.Dial(); err != nil {
continue
}
p.pools = append(p.pools, rp)
p.entropy[i] = 0
p.status[i] = true
p.servers[i] = s[i].Host
}
p.index = 0
if len(p.pools) > p.index {
p.pool = p.pools[p.index]
}
return p, nil
}
func newTimeoutPool(s Server, connTimeout, readTimeout, writeTimeout time.Duration) (*redis.Pool, error) {
return &redis.Pool{
Dial: func() (redis.Conn, error) {
conn, err := redis.Dial("tcp", fmt.Sprintf("%s:%s", s.Host, s.Port),
redis.DialConnectTimeout(connTimeout),
redis.DialReadTimeout(readTimeout),
redis.DialWriteTimeout(writeTimeout))
if err != nil {
return nil, err
}
if !(s.Auth == "" || s.Auth == "nil") {
_, err = conn.Do("AUTH", s.Auth)
if err != nil {
conn.Close()
return nil, err
}
}
if s.DB != "" {
_, err = conn.Do("SELECT", s.DB)
if err != nil {
conn.Close()
return nil, err
}
}
return conn, nil
},
TestOnBorrow: func(conn redis.Conn, _ time.Time) error {
_, err := conn.Do("PING")
return err
},
MaxIdle: 5,
IdleTimeout: 600 * time.Second,
}, nil
}