-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathsentinel.go
More file actions
80 lines (70 loc) · 1.63 KB
/
Copy pathsentinel.go
File metadata and controls
80 lines (70 loc) · 1.63 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package sentinel
import (
"runtime"
"time"
"github.com/FZambia/sentinel"
"github.com/gomodule/redigo/redis"
"github.com/fat39/redigo/v2"
)
type sentinelMode struct {
pool *redis.Pool
}
func (sm *sentinelMode) GetConn() redis.Conn {
return sm.pool.Get()
}
func (sm *sentinelMode) NewConn() (redis.Conn, error) {
return sm.pool.Dial()
}
func (sm *sentinelMode) Close() error {
return sm.pool.Close()
}
func (sm *sentinelMode) String() string { return "sentinel" }
func New(optFuncs ...OptFunc) redigo.ModeInterface {
opts := options{
addrs: []string{
"127.0.0.1:26379",
},
masterName: "mymaster",
poolOpts: redigo.DefaultPoolOpts(),
dialOpts: redigo.DefaultDialOpts(),
}
for _, optFunc := range optFuncs {
optFunc(&opts)
}
if len(opts.sentinelDialOpts) == 0 {
opts.sentinelDialOpts = opts.dialOpts
}
st := &sentinel.Sentinel{
Addrs: opts.addrs,
MasterName: opts.masterName,
Pool: func(addr string) *redis.Pool {
return &redis.Pool{
Wait: true,
MaxIdle: runtime.GOMAXPROCS(0),
Dial: func() (redis.Conn, error) {
return redis.Dial("tcp", addr, opts.sentinelDialOpts...)
},
TestOnBorrow: func(c redis.Conn, t time.Time) (err error) {
_, err = c.Do("PING")
return
},
}
},
}
pool := &redis.Pool{
Dial: func() (redis.Conn, error) {
addr, err := st.MasterAddr()
if err != nil {
return nil, err
}
return redis.Dial("tcp", addr, opts.dialOpts...)
},
}
for _, poolOptFunc := range opts.poolOpts {
poolOptFunc(pool)
}
return &sentinelMode{pool: pool}
}
func NewClient(optFuncs ...OptFunc) *redigo.Client {
return redigo.New(New(optFuncs...))
}