From e5695ce867f8df67fcb256c22e62301f5cb8ef2b Mon Sep 17 00:00:00 2001 From: Tiago Peczenyj Date: Sun, 2 Jun 2024 11:08:43 +0200 Subject: [PATCH 1/2] create a ring from universal options --- example_test.go | 11 +++++++++++ universal.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++- universal_test.go | 10 ++++++++++ 3 files changed, 70 insertions(+), 1 deletion(-) diff --git a/example_test.go b/example_test.go index 28d14b65a..2f37c75d0 100644 --- a/example_test.go +++ b/example_test.go @@ -678,6 +678,17 @@ func ExampleNewUniversalClient_cluster() { rdb.Ping(ctx) } +func ExampleNewUniversalClient_ring() { + rdb := redis.NewUniversalClient(&redis.UniversalOptions{ + AddressMap: map[string]string{ + "shard1": ":7000", + "shard2": ":7001", + "shard3": ":7002", + }, + }) + rdb.Ping(ctx) +} + func ExampleClient_SlowLogGet() { if RECluster { // skip slowlog test for cluster diff --git a/universal.go b/universal.go index 275bef3d6..0395165a5 100644 --- a/universal.go +++ b/universal.go @@ -14,6 +14,9 @@ type UniversalOptions struct { // of cluster/sentinel nodes. Addrs []string + // Map of name => host:port addresses of ring shards. + AddressMap map[string]string + // ClientName will execute the `CLIENT SETNAME ClientName` command for each conn. ClientName string @@ -163,6 +166,47 @@ func (o *UniversalOptions) Failover() *FailoverOptions { } } +// Ring returns ring options created from the universal options. +func (o *UniversalOptions) Ring() *RingOptions { + if len(o.Addrs) == 0 { + o.Addrs = []string{"127.0.0.1:26379"} + } + + return &RingOptions{ + Addrs: o.AddressMap, + ClientName: o.ClientName, + + Dialer: o.Dialer, + OnConnect: o.OnConnect, + + DB: o.DB, + Protocol: o.Protocol, + Username: o.Username, + Password: o.Password, + + MaxRetries: o.MaxRetries, + MinRetryBackoff: o.MinRetryBackoff, + MaxRetryBackoff: o.MaxRetryBackoff, + + DialTimeout: o.DialTimeout, + ReadTimeout: o.ReadTimeout, + WriteTimeout: o.WriteTimeout, + // ContextTimeoutEnabled: o.ContextTimeoutEnabled, // field does not exist yet, see PR 2726 + + PoolFIFO: o.PoolFIFO, + PoolSize: o.PoolSize, + PoolTimeout: o.PoolTimeout, + MinIdleConns: o.MinIdleConns, + MaxIdleConns: o.MaxIdleConns, + ConnMaxIdleTime: o.ConnMaxIdleTime, + ConnMaxLifetime: o.ConnMaxLifetime, + + TLSConfig: o.TLSConfig, + + // DisableIndentity: o.DisableIndentity, // field does not exist yet, see PR 2726 + } +} + // Simple returns basic options created from the universal options. func (o *UniversalOptions) Simple() *Options { addr := "127.0.0.1:6379" @@ -236,12 +280,16 @@ var ( // // 1. If the MasterName option is specified, a sentinel-backed FailoverClient is returned. // 2. if the number of Addrs is two or more, a ClusterClient is returned. -// 3. Otherwise, a single-node Client is returned. +// 3. If the AddressMap option is specified, a Ring is returned. +// 4. Otherwise, a single-node Client is returned. func NewUniversalClient(opts *UniversalOptions) UniversalClient { if opts.MasterName != "" { return NewFailoverClient(opts.Failover()) } else if len(opts.Addrs) > 1 { return NewClusterClient(opts.Cluster()) + } else if len(opts.AddressMap) > 0 { + return NewRing(opts.Ring()) } + return NewClient(opts.Simple()) } diff --git a/universal_test.go b/universal_test.go index 747c68acb..c7a1e290e 100644 --- a/universal_test.go +++ b/universal_test.go @@ -38,4 +38,14 @@ var _ = Describe("UniversalClient", func() { }) Expect(client.Ping(ctx).Err()).NotTo(HaveOccurred()) }) + + It("should connect to ring", func() { + Skip("For some reason the ring tests are skipped") + ring := redisRingOptions() + + client = redis.NewUniversalClient(&redis.UniversalOptions{ + AddressMap: ring.Addrs, + }) + Expect(client.Ping(ctx).Err()).NotTo(HaveOccurred()) + }) }) From 214aec5fa87610841ead8a9669de08c7fea5bcd2 Mon Sep 17 00:00:00 2001 From: Tiago Peczenyj Date: Sun, 2 Jun 2024 11:11:03 +0200 Subject: [PATCH 2/2] add field ContextTimeoutEnabled define in PR 2726 --- universal.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/universal.go b/universal.go index 0395165a5..e343c435e 100644 --- a/universal.go +++ b/universal.go @@ -188,10 +188,10 @@ func (o *UniversalOptions) Ring() *RingOptions { MinRetryBackoff: o.MinRetryBackoff, MaxRetryBackoff: o.MaxRetryBackoff, - DialTimeout: o.DialTimeout, - ReadTimeout: o.ReadTimeout, - WriteTimeout: o.WriteTimeout, - // ContextTimeoutEnabled: o.ContextTimeoutEnabled, // field does not exist yet, see PR 2726 + DialTimeout: o.DialTimeout, + ReadTimeout: o.ReadTimeout, + WriteTimeout: o.WriteTimeout, + ContextTimeoutEnabled: o.ContextTimeoutEnabled, PoolFIFO: o.PoolFIFO, PoolSize: o.PoolSize,