@@ -14,6 +14,9 @@ type UniversalOptions struct {
1414 // of cluster/sentinel nodes.
1515 Addrs []string
1616
17+ // Map of name => host:port addresses of ring shards.
18+ AddressMap map [string ]string
19+
1720 // ClientName will execute the `CLIENT SETNAME ClientName` command for each conn.
1821 ClientName string
1922
@@ -163,6 +166,47 @@ func (o *UniversalOptions) Failover() *FailoverOptions {
163166 }
164167}
165168
169+ // Ring returns ring options created from the universal options.
170+ func (o * UniversalOptions ) Ring () * RingOptions {
171+ if len (o .Addrs ) == 0 {
172+ o .Addrs = []string {"127.0.0.1:26379" }
173+ }
174+
175+ return & RingOptions {
176+ Addrs : o .AddressMap ,
177+ ClientName : o .ClientName ,
178+
179+ Dialer : o .Dialer ,
180+ OnConnect : o .OnConnect ,
181+
182+ DB : o .DB ,
183+ Protocol : o .Protocol ,
184+ Username : o .Username ,
185+ Password : o .Password ,
186+
187+ MaxRetries : o .MaxRetries ,
188+ MinRetryBackoff : o .MinRetryBackoff ,
189+ MaxRetryBackoff : o .MaxRetryBackoff ,
190+
191+ DialTimeout : o .DialTimeout ,
192+ ReadTimeout : o .ReadTimeout ,
193+ WriteTimeout : o .WriteTimeout ,
194+ // ContextTimeoutEnabled: o.ContextTimeoutEnabled, // field does not exist yet, see PR 2726
195+
196+ PoolFIFO : o .PoolFIFO ,
197+ PoolSize : o .PoolSize ,
198+ PoolTimeout : o .PoolTimeout ,
199+ MinIdleConns : o .MinIdleConns ,
200+ MaxIdleConns : o .MaxIdleConns ,
201+ ConnMaxIdleTime : o .ConnMaxIdleTime ,
202+ ConnMaxLifetime : o .ConnMaxLifetime ,
203+
204+ TLSConfig : o .TLSConfig ,
205+
206+ // DisableIndentity: o.DisableIndentity, // field does not exist yet, see PR 2726
207+ }
208+ }
209+
166210// Simple returns basic options created from the universal options.
167211func (o * UniversalOptions ) Simple () * Options {
168212 addr := "127.0.0.1:6379"
@@ -236,12 +280,16 @@ var (
236280//
237281// 1. If the MasterName option is specified, a sentinel-backed FailoverClient is returned.
238282// 2. if the number of Addrs is two or more, a ClusterClient is returned.
239- // 3. Otherwise, a single-node Client is returned.
283+ // 3. If the AddressMap option is specified, a Ring is returned.
284+ // 4. Otherwise, a single-node Client is returned.
240285func NewUniversalClient (opts * UniversalOptions ) UniversalClient {
241286 if opts .MasterName != "" {
242287 return NewFailoverClient (opts .Failover ())
243288 } else if len (opts .Addrs ) > 1 {
244289 return NewClusterClient (opts .Cluster ())
290+ } else if len (opts .AddressMap ) > 0 {
291+ return NewRing (opts .Ring ())
245292 }
293+
246294 return NewClient (opts .Simple ())
247295}
0 commit comments