-
Notifications
You must be signed in to change notification settings - Fork 193
/
Copy pathstandalone.go
143 lines (125 loc) · 3.57 KB
/
standalone.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
package rueidis
import (
"context"
"math/rand/v2"
"time"
"github.com/redis/rueidis/internal/cmds"
)
func newStandaloneClient(opt *ClientOption, connFn connFn, retryer retryHandler) (*standalone, error) {
if len(opt.InitAddress) == 0 {
return nil, ErrNoAddr
}
p := connFn(opt.InitAddress[0], opt)
if err := p.Dial(); err != nil {
return nil, err
}
s := &standalone{
toReplicas: opt.SendToReplicas,
primary: newSingleClientWithConn(p, cmds.NewBuilder(cmds.NoSlot), !opt.DisableRetry, opt.DisableCache, retryer),
replicas: make([]*singleClient, len(opt.Standalone.ReplicaAddress)),
}
opt.ReplicaOnly = true
for i := range s.replicas {
replicaConn := connFn(opt.Standalone.ReplicaAddress[i], opt)
if err := replicaConn.Dial(); err != nil {
s.primary.Close() // close primary if any replica fails
for j := 0; j < i; j++ {
s.replicas[j].Close()
}
return nil, err
}
s.replicas[i] = newSingleClientWithConn(replicaConn, cmds.NewBuilder(cmds.NoSlot), !opt.DisableRetry, opt.DisableCache, retryer)
}
return s, nil
}
type standalone struct {
toReplicas func(Completed) bool
primary *singleClient
replicas []*singleClient
}
func (s *standalone) B() Builder {
return s.primary.B()
}
func (s *standalone) pick() int {
if len(s.replicas) == 1 {
return 0
}
return rand.IntN(len(s.replicas))
}
func (s *standalone) Do(ctx context.Context, cmd Completed) (resp RedisResult) {
if s.toReplicas(cmd) {
return s.replicas[s.pick()].Do(ctx, cmd)
}
return s.primary.Do(ctx, cmd)
}
func (s *standalone) DoMulti(ctx context.Context, multi ...Completed) (resp []RedisResult) {
toReplica := true
for _, cmd := range multi {
if !s.toReplicas(cmd) {
toReplica = false
break
}
}
if toReplica {
return s.replicas[s.pick()].DoMulti(ctx, multi...)
}
return s.primary.DoMulti(ctx, multi...)
}
func (s *standalone) Receive(ctx context.Context, subscribe Completed, fn func(msg PubSubMessage)) error {
if s.toReplicas(subscribe) {
return s.replicas[s.pick()].Receive(ctx, subscribe, fn)
}
return s.primary.Receive(ctx, subscribe, fn)
}
func (s *standalone) Close() {
s.primary.Close()
for _, replica := range s.replicas {
replica.Close()
}
}
func (s *standalone) DoCache(ctx context.Context, cmd Cacheable, ttl time.Duration) (resp RedisResult) {
return s.primary.DoCache(ctx, cmd, ttl)
}
func (s *standalone) DoMultiCache(ctx context.Context, multi ...CacheableTTL) (resp []RedisResult) {
return s.primary.DoMultiCache(ctx, multi...)
}
func (s *standalone) DoStream(ctx context.Context, cmd Completed) RedisResultStream {
if s.toReplicas(cmd) {
return s.replicas[s.pick()].DoStream(ctx, cmd)
}
return s.primary.DoStream(ctx, cmd)
}
func (s *standalone) DoMultiStream(ctx context.Context, multi ...Completed) MultiRedisResultStream {
toReplica := true
for _, cmd := range multi {
if !s.toReplicas(cmd) {
toReplica = false
break
}
}
if toReplica {
return s.replicas[s.pick()].DoMultiStream(ctx, multi...)
}
return s.primary.DoMultiStream(ctx, multi...)
}
func (s *standalone) Dedicated(fn func(DedicatedClient) error) (err error) {
return s.primary.Dedicated(fn)
}
func (s *standalone) Dedicate() (client DedicatedClient, cancel func()) {
return s.primary.Dedicate()
}
func (s *standalone) Nodes() map[string]Client {
nodes := make(map[string]Client, len(s.replicas)+1)
for addr, client := range s.primary.Nodes() {
nodes[addr] = client
}
for _, replica := range s.replicas {
for addr, client := range replica.Nodes() {
nodes[addr] = client
}
}
return nodes
}
func (s *standalone) Mode() ClientMode {
return ClientModeStandalone
}