-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
132 lines (112 loc) · 2.57 KB
/
client.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
package sentinel
import (
"context"
"fmt"
"github.com/go-redis/redis/v8"
"github.com/keva-dev/go-sentinel/tools"
)
func newRedisClient(addr string) *redisClient {
cl := redis.NewClient(&redis.Options{
Addr: addr,
})
return &redisClient{
cl: cl,
}
}
// Waiting for https://github.com/redis/redis/issues/2416 so we can do `info server replication`
func (c *redisClient) Info() (string, error) {
str := c.cl.Info(context.Background())
err := str.Err()
if err != nil {
return "", err
}
return str.Result()
}
func (c *redisClient) Ping() (string, error) {
sttcmd := c.cl.Ping(context.Background())
if sttcmd.Err() != nil {
return "", sttcmd.Err()
}
return sttcmd.Result()
}
func (c *redisClient) SubscribeHelloChan() HelloChan {
ps := c.cl.Subscribe(context.Background(), helloChan)
return pubsubHelloChan{
internal: ps,
cl: c.cl,
}
}
func (c *redisClient) SlaveOfNoOne() error {
return c.SlaveOf("NO", "ONE")
}
func (c *redisClient) SlaveOf(host, port string) error {
sttcmd := c.cl.SlaveOf(context.Background(), host, port)
if sttcmd.Err() != nil {
return sttcmd.Err()
}
_, err := sttcmd.Result()
return err
}
// SlaveOfNoOne() error
// SlaveOf(host, port string) error
type pubsubHelloChan struct {
internal *redis.PubSub
cl *redis.Client
}
func (c pubsubHelloChan) Close() error {
return c.internal.Close()
}
func (c pubsubHelloChan) Publish(str string) error {
intcmd := c.cl.Publish(context.Background(), helloChan, str)
if intcmd.Err() != nil {
return intcmd.Err()
}
return nil
}
func (c pubsubHelloChan) Receive() (string, error) {
msg, ok := <-c.internal.Channel()
if !ok {
return "", fmt.Errorf("pubsub closed")
}
return msg.Payload, nil
}
type redisClient struct {
cl *redis.Client
}
// Close() error
// Publish(string) error
// Receive() (string, error)
var (
helloChan = "__sentinel__:hello"
)
type InternalClient interface {
Info() (string, error)
Ping() (string, error)
SubscribeHelloChan() HelloChan
SlaveOfNoOne() error
SlaveOf(host, port string) error
}
type HelloChan interface {
Close() error
Publish(string) error
Receive() (string, error)
}
type NoOpClient struct{}
func (c *NoOpClient) Ping() (string, error) { return "", nil }
func (c *NoOpClient) Info() (string, error) {
return "", nil
}
func (c *NoOpClient) SlaveOf(addr, port string) error {
return nil
}
func (c *NoOpClient) SlaveOfNoOne() error {
return nil
}
func (c *NoOpClient) SubscribeHelloChan() HelloChan {
return nil
}
func GenInterface() {
tools.MockInterfaces("sentinel", ".", map[string]interface{}{
"internal_client": &NoOpClient{},
})
}