Skip to content

Commit d678a01

Browse files
committed
Add FailoverClusterClient support + fix example/hset-struct go.sum
1 parent 11efd6a commit d678a01

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

example/hset-struct/go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
22
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
3-
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
4-
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
53
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
64
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
75
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=

universal.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@ func (o *UniversalOptions) Failover() *FailoverOptions {
154154
SentinelUsername: o.SentinelUsername,
155155
SentinelPassword: o.SentinelPassword,
156156

157+
RouteByLatency: o.RouteByLatency,
158+
RouteRandomly: o.RouteRandomly,
159+
157160
MaxRetries: o.MaxRetries,
158161
MinRetryBackoff: o.MinRetryBackoff,
159162
MaxRetryBackoff: o.MaxRetryBackoff,
@@ -260,10 +263,14 @@ var (
260263
// 2. if the number of Addrs is two or more, a ClusterClient is returned.
261264
// 3. Otherwise, a single-node Client is returned.
262265
func NewUniversalClient(opts *UniversalOptions) UniversalClient {
263-
if opts.MasterName != "" {
266+
switch {
267+
case opts.MasterName != "" && (opts.RouteByLatency || opts.RouteRandomly):
268+
return NewFailoverClusterClient(opts.Failover())
269+
case opts.MasterName != "":
264270
return NewFailoverClient(opts.Failover())
265-
} else if len(opts.Addrs) > 1 || opts.IsClusterMode {
271+
case len(opts.Addrs) > 1 || opts.IsClusterMode:
266272
return NewClusterClient(opts.Cluster())
273+
default:
274+
return NewClient(opts.Simple())
267275
}
268-
return NewClient(opts.Simple())
269276
}

universal_test.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ var _ = Describe("UniversalClient", func() {
2424
Expect(client.Ping(ctx).Err()).NotTo(HaveOccurred())
2525
})
2626

27+
It("should connect to failover cluster", Label("NonRedisEnterprise"), func() {
28+
client = redis.NewUniversalClient(&redis.UniversalOptions{
29+
MasterName: sentinelName,
30+
RouteRandomly: true,
31+
Addrs: sentinelAddrs,
32+
})
33+
_, ok := client.(*redis.ClusterClient)
34+
Expect(ok).To(BeTrue(), "expected a ClusterClient")
35+
})
36+
2737
It("should connect to simple servers", func() {
2838
client = redis.NewUniversalClient(&redis.UniversalOptions{
2939
Addrs: []string{redisAddr},
@@ -79,6 +89,7 @@ var _ = Describe("UniversalClient", func() {
7989
err = client.Set(ctx, "somekey", "somevalue", 0).Err()
8090
Expect(err).To(HaveOccurred())
8191
})
92+
8293
It("should connect to clusters if IsClusterMode is set even if only a single address is provided", Label("NonRedisEnterprise"), func() {
8394
client = redis.NewUniversalClient(&redis.UniversalOptions{
8495
Addrs: []string{cluster.addrs()[0]},
@@ -96,4 +107,3 @@ var _ = Describe("UniversalClient", func() {
96107
Expect(client.ClusterSlots(ctx).Val()).To(HaveLen(3))
97108
})
98109
})
99-

0 commit comments

Comments
 (0)