From 7d551185545fc6ad34e859963f54c5128d9c0f94 Mon Sep 17 00:00:00 2001 From: Rushikesh Joshi Date: Wed, 26 Feb 2025 08:25:55 -0800 Subject: [PATCH] feat: support Elasticache cluster mode by introducing IsClusterMode config param (#3255) Co-authored-by: Rushikesh Joshi Co-authored-by: Nedyalko Dyakov --- universal.go | 5 ++++- universal_test.go | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/universal.go b/universal.go index 47fda276..21867ec2 100644 --- a/universal.go +++ b/universal.go @@ -69,6 +69,9 @@ type UniversalOptions struct { DisableIndentity bool IdentitySuffix string UnstableResp3 bool + + // IsClusterMode can be used when only one Addrs is provided (e.g. Elasticache supports setting up cluster mode with configuration endpoint). + IsClusterMode bool } // Cluster returns cluster options created from the universal options. @@ -244,7 +247,7 @@ var ( func NewUniversalClient(opts *UniversalOptions) UniversalClient { if opts.MasterName != "" { return NewFailoverClient(opts.Failover()) - } else if len(opts.Addrs) > 1 { + } else if len(opts.Addrs) > 1 || opts.IsClusterMode { return NewClusterClient(opts.Cluster()) } return NewClient(opts.Simple()) diff --git a/universal_test.go b/universal_test.go index 9328b477..ba04324f 100644 --- a/universal_test.go +++ b/universal_test.go @@ -60,4 +60,21 @@ var _ = Describe("UniversalClient", func() { a := func() { client.FTInfo(ctx, "all").Result() } Expect(a).ToNot(Panic()) }) + + It("should connect to clusters if IsClusterMode is set even if only a single address is provided", Label("NonRedisEnterprise"), func() { + client = redis.NewUniversalClient(&redis.UniversalOptions{ + Addrs: []string{cluster.addrs()[0]}, + IsClusterMode: true, + }) + _, ok := client.(*redis.ClusterClient) + Expect(ok).To(BeTrue(), "expected a ClusterClient") + }) + + It("should return all slots after instantiating UniversalClient with IsClusterMode", Label("NonRedisEnterprise"), func() { + client = redis.NewUniversalClient(&redis.UniversalOptions{ + Addrs: []string{cluster.addrs()[0]}, + IsClusterMode: true, + }) + Expect(client.ClusterSlots(ctx).Val()).To(HaveLen(3)) + }) })