Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions redisconn/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type Opts struct {
// If IOTimeout < 0, then timeout is disabled
IOTimeout time.Duration
// DialTimeout is timeout for net.Dialer
// If it is <= 0 or >= IOTimeout, then IOTimeout
// If it is <= 0, then IOTimeout
// If IOTimeout is disabled, then 5 seconds used (but without affect on ReconnectPause)
DialTimeout time.Duration
// ReconnectPause is a pause after failed connection attempt before next one.
Expand Down Expand Up @@ -135,7 +135,7 @@ func Connect(ctx context.Context, addr string, opts Opts) (conn *Connection, err
conn.opts.IOTimeout = 0
}

if conn.opts.DialTimeout <= 0 || conn.opts.DialTimeout > conn.opts.IOTimeout {
if conn.opts.DialTimeout <= 0 {
conn.opts.DialTimeout = conn.opts.IOTimeout
}

Expand Down Expand Up @@ -520,7 +520,7 @@ func (conn *Connection) dial() error {
timeout := conn.opts.DialTimeout
tlsEnabled := conn.opts.TLSEnabled
tlsConfig := conn.opts.TLSConfig
if timeout <= 0 || timeout > 5*time.Second {
if timeout <= 0 {
timeout = 5 * time.Second
}
if address[0] == '.' || address[0] == '/' {
Expand Down
38 changes: 38 additions & 0 deletions redisconn/opts_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package redisconn

import (
"context"
"testing"
"time"

"github.com/stretchr/testify/require"
)

func TestDialTimeoutAboveIOTimeout(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

conn, err := Connect(ctx, "127.0.0.1:1", Opts{
IOTimeout: 200 * time.Millisecond,
DialTimeout: 3 * time.Second,
AsyncDial: true,
})
require.NoError(t, err)
defer conn.Close()

require.Equal(t, 3*time.Second, conn.opts.DialTimeout)
}

func TestDialTimeoutDefaultsToIOTimeout(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

conn, err := Connect(ctx, "127.0.0.1:1", Opts{
IOTimeout: 200 * time.Millisecond,
AsyncDial: true,
})
require.NoError(t, err)
defer conn.Close()

require.Equal(t, 200*time.Millisecond, conn.opts.DialTimeout)
}
Loading