Skip to content

Commit

Permalink
feat: support Elasticache cluster mode by introducing IsClusterMode c…
Browse files Browse the repository at this point in the history
…onfig param
  • Loading branch information
joshirushikesh committed Feb 22, 2025
1 parent 30e7388 commit 8dbeb49
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
5 changes: 4 additions & 1 deletion universal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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())
Expand Down
17 changes: 17 additions & 0 deletions universal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
})

0 comments on commit 8dbeb49

Please sign in to comment.