Skip to content

Commit b657760

Browse files
committed
Tweak number of retries
1 parent 513fcfb commit b657760

File tree

8 files changed

+158
-147
lines changed

8 files changed

+158
-147
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ ring := redis.NewRing(&redis.RingOptions{
2626
})
2727
```
2828

29+
- `ClusterOptions.MaxRedirects` default value is changed from 8 to 3.
30+
- `Options.MaxRetries` default value is changed from 0 to 3.
31+
2932
- `Cluster.ForEachNode` is renamed to `ForEachShard` for consistency with `Ring`.
3033

3134
## v7.3

cluster.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ type ClusterOptions struct {
3232

3333
// The maximum number of retries before giving up. Command is retried
3434
// on network errors and MOVED/ASK redirects.
35-
// Default is 8 retries.
35+
// Default is 3 retries.
3636
MaxRedirects int
3737

3838
// Enables read-only commands on slave nodes.
@@ -83,7 +83,7 @@ func (opt *ClusterOptions) init() {
8383
if opt.MaxRedirects == -1 {
8484
opt.MaxRedirects = 0
8585
} else if opt.MaxRedirects == 0 {
86-
opt.MaxRedirects = 8
86+
opt.MaxRedirects = 3
8787
}
8888

8989
if (opt.RouteByLatency || opt.RouteRandomly) && opt.ClusterSlots == nil {
@@ -107,6 +107,9 @@ func (opt *ClusterOptions) init() {
107107
opt.WriteTimeout = opt.ReadTimeout
108108
}
109109

110+
if opt.MaxRetries == 0 {
111+
opt.MaxRetries = -1
112+
}
110113
switch opt.MinRetryBackoff {
111114
case -1:
112115
opt.MinRetryBackoff = 0
@@ -132,12 +135,12 @@ func (opt *ClusterOptions) clientOptions() *Options {
132135
Dialer: opt.Dialer,
133136
OnConnect: opt.OnConnect,
134137

138+
Username: opt.Username,
139+
Password: opt.Password,
140+
135141
MaxRetries: opt.MaxRetries,
136142
MinRetryBackoff: opt.MinRetryBackoff,
137143
MaxRetryBackoff: opt.MaxRetryBackoff,
138-
Username: opt.Username,
139-
Password: opt.Password,
140-
readOnly: opt.ReadOnly,
141144

142145
DialTimeout: opt.DialTimeout,
143146
ReadTimeout: opt.ReadTimeout,
@@ -150,6 +153,8 @@ func (opt *ClusterOptions) clientOptions() *Options {
150153
IdleTimeout: opt.IdleTimeout,
151154
IdleCheckFrequency: disableIdleCheck,
152155

156+
readOnly: opt.ReadOnly,
157+
153158
TLSConfig: opt.TLSConfig,
154159
}
155160
}

main_test.go

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,15 @@ func TestGinkgoSuite(t *testing.T) {
121121

122122
func redisOptions() *redis.Options {
123123
return &redis.Options{
124-
Addr: redisAddr,
125-
DB: 15,
126-
DialTimeout: 10 * time.Second,
127-
ReadTimeout: 30 * time.Second,
128-
WriteTimeout: 30 * time.Second,
124+
Addr: redisAddr,
125+
DB: 15,
126+
127+
DialTimeout: 10 * time.Second,
128+
ReadTimeout: 30 * time.Second,
129+
WriteTimeout: 30 * time.Second,
130+
131+
MaxRetries: -1,
132+
129133
PoolSize: 10,
130134
PoolTimeout: 30 * time.Second,
131135
IdleTimeout: time.Minute,
@@ -135,9 +139,12 @@ func redisOptions() *redis.Options {
135139

136140
func redisClusterOptions() *redis.ClusterOptions {
137141
return &redis.ClusterOptions{
138-
DialTimeout: 10 * time.Second,
139-
ReadTimeout: 30 * time.Second,
140-
WriteTimeout: 30 * time.Second,
142+
DialTimeout: 10 * time.Second,
143+
ReadTimeout: 30 * time.Second,
144+
WriteTimeout: 30 * time.Second,
145+
146+
MaxRedirects: 8,
147+
141148
PoolSize: 10,
142149
PoolTimeout: 30 * time.Second,
143150
IdleTimeout: time.Minute,
@@ -151,9 +158,13 @@ func redisRingOptions() *redis.RingOptions {
151158
"ringShardOne": ":" + ringShard1Port,
152159
"ringShardTwo": ":" + ringShard2Port,
153160
},
154-
DialTimeout: 10 * time.Second,
155-
ReadTimeout: 30 * time.Second,
156-
WriteTimeout: 30 * time.Second,
161+
162+
DialTimeout: 10 * time.Second,
163+
ReadTimeout: 30 * time.Second,
164+
WriteTimeout: 30 * time.Second,
165+
166+
MaxRetries: -1,
167+
157168
PoolSize: 10,
158169
PoolTimeout: 30 * time.Second,
159170
IdleTimeout: time.Minute,
@@ -233,7 +244,8 @@ func execCmd(name string, args ...string) (*os.Process, error) {
233244

234245
func connectTo(port string) (*redis.Client, error) {
235246
client := redis.NewClient(&redis.Options{
236-
Addr: ":" + port,
247+
Addr: ":" + port,
248+
MaxRetries: -1,
237249
})
238250

239251
err := eventually(func() error {

options.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ type Options struct {
5757
DB int
5858

5959
// Maximum number of retries before giving up.
60-
// Default is to not retry failed commands.
60+
// Default is 3 retries.
6161
MaxRetries int
6262
// Minimum backoff between each retry.
6363
// Default is 8 milliseconds; -1 disables backoff.
@@ -164,6 +164,8 @@ func (opt *Options) init() {
164164

165165
if opt.MaxRetries == -1 {
166166
opt.MaxRetries = 0
167+
} else if opt.MaxRetries == 0 {
168+
opt.MaxRetries = 3
167169
}
168170
switch opt.MinRetryBackoff {
169171
case -1:

redis_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ var _ = Describe("Client", func() {
215215
It("should retry with backoff", func() {
216216
clientNoRetry := redis.NewClient(&redis.Options{
217217
Addr: ":1234",
218-
MaxRetries: 0,
218+
MaxRetries: -1,
219219
})
220220
defer clientNoRetry.Close()
221221

ring.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package redis
22

33
import (
44
"context"
5+
"crypto/tls"
56
"errors"
67
"fmt"
78
"net"
@@ -84,6 +85,9 @@ type RingOptions struct {
8485
PoolTimeout time.Duration
8586
IdleTimeout time.Duration
8687
IdleCheckFrequency time.Duration
88+
89+
TLSConfig *tls.Config
90+
Limiter Limiter
8791
}
8892

8993
func (opt *RingOptions) init() {
@@ -101,6 +105,11 @@ func (opt *RingOptions) init() {
101105
opt.NewConsistentHash = newRendezvous
102106
}
103107

108+
if opt.MaxRetries == -1 {
109+
opt.MaxRetries = 0
110+
} else if opt.MaxRetries == 0 {
111+
opt.MaxRetries = 3
112+
}
104113
switch opt.MinRetryBackoff {
105114
case -1:
106115
opt.MinRetryBackoff = 0
@@ -124,6 +133,8 @@ func (opt *RingOptions) clientOptions() *Options {
124133
Password: opt.Password,
125134
DB: opt.DB,
126135

136+
MaxRetries: -1,
137+
127138
DialTimeout: opt.DialTimeout,
128139
ReadTimeout: opt.ReadTimeout,
129140
WriteTimeout: opt.WriteTimeout,
@@ -134,6 +145,9 @@ func (opt *RingOptions) clientOptions() *Options {
134145
PoolTimeout: opt.PoolTimeout,
135146
IdleTimeout: opt.IdleTimeout,
136147
IdleCheckFrequency: opt.IdleCheckFrequency,
148+
149+
TLSConfig: opt.TLSConfig,
150+
Limiter: opt.Limiter,
137151
}
138152
}
139153

0 commit comments

Comments
 (0)