Skip to content

Commit c25ee1e

Browse files
authored
Merge branch 'master' into do_cmd_for_simple_conn
2 parents dd8d920 + 9762559 commit c25ee1e

File tree

8 files changed

+52
-9
lines changed

8 files changed

+52
-9
lines changed

.github/workflows/golangci-lint.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,6 @@ jobs:
2222
- uses: actions/checkout@v4
2323
- name: golangci-lint
2424
uses: golangci/[email protected]
25+
with:
26+
verify: false # disable verifying the configuration since golangci is currently introducing breaking changes in the configuration
27+

error.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ func shouldRetry(err error, retryTimeout bool) bool {
7575
if strings.HasPrefix(s, "READONLY ") {
7676
return true
7777
}
78+
if strings.HasPrefix(s, "MASTERDOWN ") {
79+
return true
80+
}
7881
if strings.HasPrefix(s, "CLUSTERDOWN ") {
7982
return true
8083
}

internal/pool/bench_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func BenchmarkPoolGetPut(b *testing.B) {
3333
Dialer: dummyDialer,
3434
PoolSize: bm.poolSize,
3535
PoolTimeout: time.Second,
36+
DialTimeout: 1 * time.Second,
3637
ConnMaxIdleTime: time.Hour,
3738
})
3839

@@ -76,6 +77,7 @@ func BenchmarkPoolGetRemove(b *testing.B) {
7677
Dialer: dummyDialer,
7778
PoolSize: bm.poolSize,
7879
PoolTimeout: time.Second,
80+
DialTimeout: 1 * time.Second,
7981
ConnMaxIdleTime: time.Hour,
8082
})
8183

internal/pool/pool.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ type Options struct {
6262

6363
PoolFIFO bool
6464
PoolSize int
65+
DialTimeout time.Duration
6566
PoolTimeout time.Duration
6667
MinIdleConns int
6768
MaxIdleConns int
@@ -140,7 +141,10 @@ func (p *ConnPool) checkMinIdleConns() {
140141
}
141142

142143
func (p *ConnPool) addIdleConn() error {
143-
cn, err := p.dialConn(context.TODO(), true)
144+
ctx, cancel := context.WithTimeout(context.Background(), p.cfg.DialTimeout)
145+
defer cancel()
146+
147+
cn, err := p.dialConn(ctx, true)
144148
if err != nil {
145149
return err
146150
}
@@ -230,15 +234,19 @@ func (p *ConnPool) tryDial() {
230234
return
231235
}
232236

233-
conn, err := p.cfg.Dialer(context.Background())
237+
ctx, cancel := context.WithTimeout(context.Background(), p.cfg.DialTimeout)
238+
239+
conn, err := p.cfg.Dialer(ctx)
234240
if err != nil {
235241
p.setLastDialError(err)
236242
time.Sleep(time.Second)
243+
cancel()
237244
continue
238245
}
239246

240247
atomic.StoreUint32(&p.dialErrorsNum, 0)
241248
_ = conn.Close()
249+
cancel()
242250
return
243251
}
244252
}

internal/pool/pool_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ var _ = Describe("ConnPool", func() {
2222
Dialer: dummyDialer,
2323
PoolSize: 10,
2424
PoolTimeout: time.Hour,
25+
DialTimeout: 1 * time.Second,
2526
ConnMaxIdleTime: time.Millisecond,
2627
})
2728
})
@@ -46,6 +47,7 @@ var _ = Describe("ConnPool", func() {
4647
},
4748
PoolSize: 10,
4849
PoolTimeout: time.Hour,
50+
DialTimeout: 1 * time.Second,
4951
ConnMaxIdleTime: time.Millisecond,
5052
MinIdleConns: minIdleConns,
5153
})
@@ -129,6 +131,7 @@ var _ = Describe("MinIdleConns", func() {
129131
PoolSize: poolSize,
130132
MinIdleConns: minIdleConns,
131133
PoolTimeout: 100 * time.Millisecond,
134+
DialTimeout: 1 * time.Second,
132135
ConnMaxIdleTime: -1,
133136
})
134137
Eventually(func() int {
@@ -306,6 +309,7 @@ var _ = Describe("race", func() {
306309
Dialer: dummyDialer,
307310
PoolSize: 10,
308311
PoolTimeout: time.Minute,
312+
DialTimeout: 1 * time.Second,
309313
ConnMaxIdleTime: time.Millisecond,
310314
})
311315

@@ -336,6 +340,7 @@ var _ = Describe("race", func() {
336340
PoolSize: 1000,
337341
MinIdleConns: 50,
338342
PoolTimeout: 3 * time.Second,
343+
DialTimeout: 1 * time.Second,
339344
}
340345
p := pool.NewConnPool(opt)
341346

options.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,7 @@ func newConnPool(
531531
PoolFIFO: opt.PoolFIFO,
532532
PoolSize: opt.PoolSize,
533533
PoolTimeout: opt.PoolTimeout,
534+
DialTimeout: opt.DialTimeout,
534535
MinIdleConns: opt.MinIdleConns,
535536
MaxIdleConns: opt.MaxIdleConns,
536537
MaxActiveConns: opt.MaxActiveConns,

universal.go

Lines changed: 17 additions & 6 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,
@@ -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

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)