Skip to content

Commit

Permalink
Tweak number of retries
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed Sep 11, 2020
1 parent 513fcfb commit b657760
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 147 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ ring := redis.NewRing(&redis.RingOptions{
})
```

- `ClusterOptions.MaxRedirects` default value is changed from 8 to 3.
- `Options.MaxRetries` default value is changed from 0 to 3.

- `Cluster.ForEachNode` is renamed to `ForEachShard` for consistency with `Ring`.

## v7.3
Expand Down
15 changes: 10 additions & 5 deletions cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type ClusterOptions struct {

// The maximum number of retries before giving up. Command is retried
// on network errors and MOVED/ASK redirects.
// Default is 8 retries.
// Default is 3 retries.
MaxRedirects int

// Enables read-only commands on slave nodes.
Expand Down Expand Up @@ -83,7 +83,7 @@ func (opt *ClusterOptions) init() {
if opt.MaxRedirects == -1 {
opt.MaxRedirects = 0
} else if opt.MaxRedirects == 0 {
opt.MaxRedirects = 8
opt.MaxRedirects = 3
}

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

if opt.MaxRetries == 0 {
opt.MaxRetries = -1
}
switch opt.MinRetryBackoff {
case -1:
opt.MinRetryBackoff = 0
Expand All @@ -132,12 +135,12 @@ func (opt *ClusterOptions) clientOptions() *Options {
Dialer: opt.Dialer,
OnConnect: opt.OnConnect,

Username: opt.Username,
Password: opt.Password,

MaxRetries: opt.MaxRetries,
MinRetryBackoff: opt.MinRetryBackoff,
MaxRetryBackoff: opt.MaxRetryBackoff,
Username: opt.Username,
Password: opt.Password,
readOnly: opt.ReadOnly,

DialTimeout: opt.DialTimeout,
ReadTimeout: opt.ReadTimeout,
Expand All @@ -150,6 +153,8 @@ func (opt *ClusterOptions) clientOptions() *Options {
IdleTimeout: opt.IdleTimeout,
IdleCheckFrequency: disableIdleCheck,

readOnly: opt.ReadOnly,

TLSConfig: opt.TLSConfig,
}
}
Expand Down
36 changes: 24 additions & 12 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,15 @@ func TestGinkgoSuite(t *testing.T) {

func redisOptions() *redis.Options {
return &redis.Options{
Addr: redisAddr,
DB: 15,
DialTimeout: 10 * time.Second,
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
Addr: redisAddr,
DB: 15,

DialTimeout: 10 * time.Second,
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,

MaxRetries: -1,

PoolSize: 10,
PoolTimeout: 30 * time.Second,
IdleTimeout: time.Minute,
Expand All @@ -135,9 +139,12 @@ func redisOptions() *redis.Options {

func redisClusterOptions() *redis.ClusterOptions {
return &redis.ClusterOptions{
DialTimeout: 10 * time.Second,
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
DialTimeout: 10 * time.Second,
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,

MaxRedirects: 8,

PoolSize: 10,
PoolTimeout: 30 * time.Second,
IdleTimeout: time.Minute,
Expand All @@ -151,9 +158,13 @@ func redisRingOptions() *redis.RingOptions {
"ringShardOne": ":" + ringShard1Port,
"ringShardTwo": ":" + ringShard2Port,
},
DialTimeout: 10 * time.Second,
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,

DialTimeout: 10 * time.Second,
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,

MaxRetries: -1,

PoolSize: 10,
PoolTimeout: 30 * time.Second,
IdleTimeout: time.Minute,
Expand Down Expand Up @@ -233,7 +244,8 @@ func execCmd(name string, args ...string) (*os.Process, error) {

func connectTo(port string) (*redis.Client, error) {
client := redis.NewClient(&redis.Options{
Addr: ":" + port,
Addr: ":" + port,
MaxRetries: -1,
})

err := eventually(func() error {
Expand Down
4 changes: 3 additions & 1 deletion options.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ type Options struct {
DB int

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

if opt.MaxRetries == -1 {
opt.MaxRetries = 0
} else if opt.MaxRetries == 0 {
opt.MaxRetries = 3
}
switch opt.MinRetryBackoff {
case -1:
Expand Down
2 changes: 1 addition & 1 deletion redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ var _ = Describe("Client", func() {
It("should retry with backoff", func() {
clientNoRetry := redis.NewClient(&redis.Options{
Addr: ":1234",
MaxRetries: 0,
MaxRetries: -1,
})
defer clientNoRetry.Close()

Expand Down
14 changes: 14 additions & 0 deletions ring.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package redis

import (
"context"
"crypto/tls"
"errors"
"fmt"
"net"
Expand Down Expand Up @@ -84,6 +85,9 @@ type RingOptions struct {
PoolTimeout time.Duration
IdleTimeout time.Duration
IdleCheckFrequency time.Duration

TLSConfig *tls.Config
Limiter Limiter
}

func (opt *RingOptions) init() {
Expand All @@ -101,6 +105,11 @@ func (opt *RingOptions) init() {
opt.NewConsistentHash = newRendezvous
}

if opt.MaxRetries == -1 {
opt.MaxRetries = 0
} else if opt.MaxRetries == 0 {
opt.MaxRetries = 3
}
switch opt.MinRetryBackoff {
case -1:
opt.MinRetryBackoff = 0
Expand All @@ -124,6 +133,8 @@ func (opt *RingOptions) clientOptions() *Options {
Password: opt.Password,
DB: opt.DB,

MaxRetries: -1,

DialTimeout: opt.DialTimeout,
ReadTimeout: opt.ReadTimeout,
WriteTimeout: opt.WriteTimeout,
Expand All @@ -134,6 +145,9 @@ func (opt *RingOptions) clientOptions() *Options {
PoolTimeout: opt.PoolTimeout,
IdleTimeout: opt.IdleTimeout,
IdleCheckFrequency: opt.IdleCheckFrequency,

TLSConfig: opt.TLSConfig,
Limiter: opt.Limiter,
}
}

Expand Down
Loading

0 comments on commit b657760

Please sign in to comment.