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 // The sentinel master name.
16 // Only failover clients.
19 // Database to be selected after connecting to the server.
20 // Only single-node and failover clients.
23 // Only cluster clients.
25 // Enables read only queries on slave nodes.
33 OnConnect func(*Conn) error
36 DialTimeout time.Duration
37 ReadTimeout time.Duration
38 WriteTimeout time.Duration
40 PoolTimeout time.Duration
41 IdleTimeout time.Duration
42 IdleCheckFrequency time.Duration
46 func (o *UniversalOptions) cluster() *ClusterOptions {
47 if len(o.Addrs) == 0 {
48 o.Addrs = []string{"127.0.0.1:6379"}
51 return &ClusterOptions{
53 MaxRedirects: o.MaxRedirects,
54 RouteByLatency: o.RouteByLatency,
57 OnConnect: o.OnConnect,
58 MaxRetries: o.MaxRetries,
60 DialTimeout: o.DialTimeout,
61 ReadTimeout: o.ReadTimeout,
62 WriteTimeout: o.WriteTimeout,
64 PoolTimeout: o.PoolTimeout,
65 IdleTimeout: o.IdleTimeout,
66 IdleCheckFrequency: o.IdleCheckFrequency,
67 TLSConfig: o.TLSConfig,
71 func (o *UniversalOptions) failover() *FailoverOptions {
72 if len(o.Addrs) == 0 {
73 o.Addrs = []string{"127.0.0.1:26379"}
76 return &FailoverOptions{
77 SentinelAddrs: o.Addrs,
78 MasterName: o.MasterName,
81 OnConnect: o.OnConnect,
82 MaxRetries: o.MaxRetries,
84 DialTimeout: o.DialTimeout,
85 ReadTimeout: o.ReadTimeout,
86 WriteTimeout: o.WriteTimeout,
88 PoolTimeout: o.PoolTimeout,
89 IdleTimeout: o.IdleTimeout,
90 IdleCheckFrequency: o.IdleCheckFrequency,
91 TLSConfig: o.TLSConfig,
95 func (o *UniversalOptions) simple() *Options {
96 addr := "127.0.0.1:6379"
105 OnConnect: o.OnConnect,
106 MaxRetries: o.MaxRetries,
107 Password: o.Password,
108 DialTimeout: o.DialTimeout,
109 ReadTimeout: o.ReadTimeout,
110 WriteTimeout: o.WriteTimeout,
111 PoolSize: o.PoolSize,
112 PoolTimeout: o.PoolTimeout,
113 IdleTimeout: o.IdleTimeout,
114 IdleCheckFrequency: o.IdleCheckFrequency,
115 TLSConfig: o.TLSConfig,
119 // --------------------------------------------------------------------
121 // UniversalClient is an abstract client which - based on the provided options -
122 // can connect to either clusters, or sentinel-backed failover instances or simple
123 // single-instance servers. This can be useful for testing cluster-specific
124 // applications locally.
125 type UniversalClient interface {
127 Watch(fn func(*Tx) error, keys ...string) error
128 Process(cmd Cmder) error
129 WrapProcess(fn func(oldProcess func(cmd Cmder) error) func(cmd Cmder) error)
130 Subscribe(channels ...string) *PubSub
131 PSubscribe(channels ...string) *PubSub
135 var _ UniversalClient = (*Client)(nil)
136 var _ UniversalClient = (*ClusterClient)(nil)
138 // NewUniversalClient returns a new multi client. The type of client returned depends
139 // on the following three conditions:
141 // 1. if a MasterName is passed a sentinel-backed FailoverClient will be returned
142 // 2. if the number of Addrs is two or more, a ClusterClient will be returned
143 // 3. otherwise, a single-node redis Client will be returned.
144 func NewUniversalClient(opts *UniversalOptions) UniversalClient {
145 if opts.MasterName != "" {
146 return NewFailoverClient(opts.failover())
147 } else if len(opts.Addrs) > 1 {
148 return NewClusterClient(opts.cluster())
150 return NewClient(opts.simple())