1 package redis
2
3 import (
4 "crypto/tls"
5 "time"
6 )
7
8
9
10 type UniversalOptions struct {
11
12
13 Addrs []string
14
15
16
17 DB int
18
19
20
21 OnConnect func(*Conn) error
22 Password string
23 MaxRetries int
24 MinRetryBackoff time.Duration
25 MaxRetryBackoff time.Duration
26 DialTimeout time.Duration
27 ReadTimeout time.Duration
28 WriteTimeout time.Duration
29 PoolSize int
30 MinIdleConns int
31 MaxConnAge time.Duration
32 PoolTimeout time.Duration
33 IdleTimeout time.Duration
34 IdleCheckFrequency time.Duration
35 TLSConfig *tls.Config
36
37
38
39 MaxRedirects int
40 ReadOnly bool
41 RouteByLatency bool
42 RouteRandomly bool
43
44
45
46 MasterName string
47 }
48
49 func (o *UniversalOptions) cluster() *ClusterOptions {
50 if len(o.Addrs) == 0 {
51 o.Addrs = []string{"127.0.0.1:6379"}
52 }
53
54 return &ClusterOptions{
55 Addrs: o.Addrs,
56 OnConnect: o.OnConnect,
57
58 Password: o.Password,
59
60 MaxRedirects: o.MaxRedirects,
61 ReadOnly: o.ReadOnly,
62 RouteByLatency: o.RouteByLatency,
63 RouteRandomly: o.RouteRandomly,
64
65 MaxRetries: o.MaxRetries,
66 MinRetryBackoff: o.MinRetryBackoff,
67 MaxRetryBackoff: o.MaxRetryBackoff,
68
69 DialTimeout: o.DialTimeout,
70 ReadTimeout: o.ReadTimeout,
71 WriteTimeout: o.WriteTimeout,
72 PoolSize: o.PoolSize,
73 MinIdleConns: o.MinIdleConns,
74 MaxConnAge: o.MaxConnAge,
75 PoolTimeout: o.PoolTimeout,
76 IdleTimeout: o.IdleTimeout,
77 IdleCheckFrequency: o.IdleCheckFrequency,
78
79 TLSConfig: o.TLSConfig,
80 }
81 }
82
83 func (o *UniversalOptions) failover() *FailoverOptions {
84 if len(o.Addrs) == 0 {
85 o.Addrs = []string{"127.0.0.1:26379"}
86 }
87
88 return &FailoverOptions{
89 SentinelAddrs: o.Addrs,
90 MasterName: o.MasterName,
91 OnConnect: o.OnConnect,
92
93 DB: o.DB,
94 Password: o.Password,
95
96 MaxRetries: o.MaxRetries,
97 MinRetryBackoff: o.MinRetryBackoff,
98 MaxRetryBackoff: o.MaxRetryBackoff,
99
100 DialTimeout: o.DialTimeout,
101 ReadTimeout: o.ReadTimeout,
102 WriteTimeout: o.WriteTimeout,
103
104 PoolSize: o.PoolSize,
105 MinIdleConns: o.MinIdleConns,
106 MaxConnAge: o.MaxConnAge,
107 PoolTimeout: o.PoolTimeout,
108 IdleTimeout: o.IdleTimeout,
109 IdleCheckFrequency: o.IdleCheckFrequency,
110
111 TLSConfig: o.TLSConfig,
112 }
113 }
114
115 func (o *UniversalOptions) simple() *Options {
116 addr := "127.0.0.1:6379"
117 if len(o.Addrs) > 0 {
118 addr = o.Addrs[0]
119 }
120
121 return &Options{
122 Addr: addr,
123 OnConnect: o.OnConnect,
124
125 DB: o.DB,
126 Password: o.Password,
127
128 MaxRetries: o.MaxRetries,
129 MinRetryBackoff: o.MinRetryBackoff,
130 MaxRetryBackoff: o.MaxRetryBackoff,
131
132 DialTimeout: o.DialTimeout,
133 ReadTimeout: o.ReadTimeout,
134 WriteTimeout: o.WriteTimeout,
135
136 PoolSize: o.PoolSize,
137 MinIdleConns: o.MinIdleConns,
138 MaxConnAge: o.MaxConnAge,
139 PoolTimeout: o.PoolTimeout,
140 IdleTimeout: o.IdleTimeout,
141 IdleCheckFrequency: o.IdleCheckFrequency,
142
143 TLSConfig: o.TLSConfig,
144 }
145 }
146
147
148
149
150
151
152
153 type UniversalClient interface {
154 Cmdable
155 Watch(fn func(*Tx) error, keys ...string) error
156 Process(cmd Cmder) error
157 WrapProcess(fn func(oldProcess func(cmd Cmder) error) func(cmd Cmder) error)
158 WrapProcessPipeline(fn func(oldProcess func([]Cmder) error) func([]Cmder) error)
159 Subscribe(channels ...string) *PubSub
160 PSubscribe(channels ...string) *PubSub
161 Close() error
162 }
163
164 var _ UniversalClient = (*Client)(nil)
165 var _ UniversalClient = (*ClusterClient)(nil)
166
167
168
169
170
171
172
173 func NewUniversalClient(opts *UniversalOptions) UniversalClient {
174 if opts.MasterName != "" {
175 return NewFailoverClient(opts.failover())
176 } else if len(opts.Addrs) > 1 {
177 return NewClusterClient(opts.cluster())
178 }
179 return NewClient(opts.simple())
180 }
181
View as plain text