Skip to content

Commit e5695ce

Browse files
committed
create a ring from universal options
1 parent 2d8fa02 commit e5695ce

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

example_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,17 @@ func ExampleNewUniversalClient_cluster() {
678678
rdb.Ping(ctx)
679679
}
680680

681+
func ExampleNewUniversalClient_ring() {
682+
rdb := redis.NewUniversalClient(&redis.UniversalOptions{
683+
AddressMap: map[string]string{
684+
"shard1": ":7000",
685+
"shard2": ":7001",
686+
"shard3": ":7002",
687+
},
688+
})
689+
rdb.Ping(ctx)
690+
}
691+
681692
func ExampleClient_SlowLogGet() {
682693
if RECluster {
683694
// skip slowlog test for cluster

universal.go

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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.
167211
func (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.
240285
func 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
}

universal_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,14 @@ var _ = Describe("UniversalClient", func() {
3838
})
3939
Expect(client.Ping(ctx).Err()).NotTo(HaveOccurred())
4040
})
41+
42+
It("should connect to ring", func() {
43+
Skip("For some reason the ring tests are skipped")
44+
ring := redisRingOptions()
45+
46+
client = redis.NewUniversalClient(&redis.UniversalOptions{
47+
AddressMap: ring.Addrs,
48+
})
49+
Expect(client.Ping(ctx).Err()).NotTo(HaveOccurred())
50+
})
4151
})

0 commit comments

Comments
 (0)