8 // UniversalOptions information is required by UniversalClient to establish
10 type UniversalOptions struct {
11 // Either a single address or a seed list of host:port addresses
12 // of cluster/sentinel nodes.
15 // Database to be selected after connecting to the server.
16 // Only single-node and failover clients.
21 OnConnect func(*Conn) error
24 MinRetryBackoff time.Duration
25 MaxRetryBackoff time.Duration
26 DialTimeout time.Duration
27 ReadTimeout time.Duration
28 WriteTimeout time.Duration
31 MaxConnAge time.Duration
32 PoolTimeout time.Duration
33 IdleTimeout time.Duration
34 IdleCheckFrequency time.Duration
37 // Only cluster clients.
44 // The sentinel master name.
45 // Only failover clients.
49 func (o *UniversalOptions) cluster() *ClusterOptions {
50 if len(o.Addrs) == 0 {
51 o.Addrs = []string{"127.0.0.1:6379"}
54 return &ClusterOptions{
56 OnConnect: o.OnConnect,
60 MaxRedirects: o.MaxRedirects,
62 RouteByLatency: o.RouteByLatency,
63 RouteRandomly: o.RouteRandomly,
65 MaxRetries: o.MaxRetries,
66 MinRetryBackoff: o.MinRetryBackoff,
67 MaxRetryBackoff: o.MaxRetryBackoff,
69 DialTimeout: o.DialTimeout,
70 ReadTimeout: o.ReadTimeout,
71 WriteTimeout: o.WriteTimeout,
73 MinIdleConns: o.MinIdleConns,
74 MaxConnAge: o.MaxConnAge,
75 PoolTimeout: o.PoolTimeout,
76 IdleTimeout: o.IdleTimeout,
77 IdleCheckFrequency: o.IdleCheckFrequency,
79 TLSConfig: o.TLSConfig,
83 func (o *UniversalOptions) failover() *FailoverOptions {
84 if len(o.Addrs) == 0 {
85 o.Addrs = []string{"127.0.0.1:26379"}
88 return &FailoverOptions{
89 SentinelAddrs: o.Addrs,
90 MasterName: o.MasterName,
91 OnConnect: o.OnConnect,
96 MaxRetries: o.MaxRetries,
97 MinRetryBackoff: o.MinRetryBackoff,
98 MaxRetryBackoff: o.MaxRetryBackoff,
100 DialTimeout: o.DialTimeout,
101 ReadTimeout: o.ReadTimeout,
102 WriteTimeout: o.WriteTimeout,
104 PoolSize: o.PoolSize,
105 MinIdleConns: o.MinIdleConns,
106 MaxConnAge: o.MaxConnAge,
107 PoolTimeout: o.PoolTimeout,
108 IdleTimeout: o.IdleTimeout,
109 IdleCheckFrequency: o.IdleCheckFrequency,
111 TLSConfig: o.TLSConfig,
115 func (o *UniversalOptions) simple() *Options {
116 addr := "127.0.0.1:6379"
117 if len(o.Addrs) > 0 {
123 OnConnect: o.OnConnect,
126 Password: o.Password,
128 MaxRetries: o.MaxRetries,
129 MinRetryBackoff: o.MinRetryBackoff,
130 MaxRetryBackoff: o.MaxRetryBackoff,
132 DialTimeout: o.DialTimeout,
133 ReadTimeout: o.ReadTimeout,
134 WriteTimeout: o.WriteTimeout,
136 PoolSize: o.PoolSize,
137 MinIdleConns: o.MinIdleConns,
138 MaxConnAge: o.MaxConnAge,
139 PoolTimeout: o.PoolTimeout,
140 IdleTimeout: o.IdleTimeout,
141 IdleCheckFrequency: o.IdleCheckFrequency,
143 TLSConfig: o.TLSConfig,
147 // --------------------------------------------------------------------
149 // UniversalClient is an abstract client which - based on the provided options -
150 // can connect to either clusters, or sentinel-backed failover instances or simple
151 // single-instance servers. This can be useful for testing cluster-specific
152 // applications locally.
153 type UniversalClient interface {
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 Subscribe(channels ...string) *PubSub
159 PSubscribe(channels ...string) *PubSub
163 var _ UniversalClient = (*Client)(nil)
164 var _ UniversalClient = (*ClusterClient)(nil)
166 // NewUniversalClient returns a new multi client. The type of client returned depends
167 // on the following three conditions:
169 // 1. if a MasterName is passed a sentinel-backed FailoverClient will be returned
170 // 2. if the number of Addrs is two or more, a ClusterClient will be returned
171 // 3. otherwise, a single-node redis Client will be returned.
172 func NewUniversalClient(opts *UniversalOptions) UniversalClient {
173 if opts.MasterName != "" {
174 return NewFailoverClient(opts.failover())
175 } else if len(opts.Addrs) > 1 {
176 return NewClusterClient(opts.cluster())
178 return NewClient(opts.simple())