Skip to content

Commit d863956

Browse files
Add FailoverClusterClient support for Universal client (#2794)
* Add FailoverClusterClient support + fix example/hset-struct go.sum * Improve NewUniversalClient comment --------- Co-authored-by: Nedyalko Dyakov <[email protected]>
1 parent 1505939 commit d863956

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

universal.go

+17-6
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,
@@ -256,14 +259,22 @@ var (
256259
// NewUniversalClient returns a new multi client. The type of the returned client depends
257260
// on the following conditions:
258261
//
259-
// 1. If the MasterName option is specified, a sentinel-backed FailoverClient is returned.
260-
// 2. if the number of Addrs is two or more, a ClusterClient is returned.
261-
// 3. Otherwise, a single-node Client is returned.
262+
// 1. If the MasterName option is specified with RouteByLatency, RouteRandomly or IsClusterMode,
263+
// a FailoverClusterClient is returned.
264+
// 2. If the MasterName option is specified without RouteByLatency, RouteRandomly or IsClusterMode,
265+
// a sentinel-backed FailoverClient is returned.
266+
// 3. If the number of Addrs is two or more, or IsClusterMode option is specified,
267+
// a ClusterClient is returned.
268+
// 4. Otherwise, a single-node Client is returned.
262269
func NewUniversalClient(opts *UniversalOptions) UniversalClient {
263-
if opts.MasterName != "" {
270+
switch {
271+
case opts.MasterName != "" && (opts.RouteByLatency || opts.RouteRandomly || opts.IsClusterMode):
272+
return NewFailoverClusterClient(opts.Failover())
273+
case opts.MasterName != "":
264274
return NewFailoverClient(opts.Failover())
265-
} else if len(opts.Addrs) > 1 || opts.IsClusterMode {
275+
case len(opts.Addrs) > 1 || opts.IsClusterMode:
266276
return NewClusterClient(opts.Cluster())
277+
default:
278+
return NewClient(opts.Simple())
267279
}
268-
return NewClient(opts.Simple())
269280
}

universal_test.go

+11-1
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)