diff --git a/redisconn/conn.go b/redisconn/conn.go index 889903f..c0531b6 100644 --- a/redisconn/conn.go +++ b/redisconn/conn.go @@ -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. @@ -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 } @@ -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] == '/' { diff --git a/redisconn/opts_internal_test.go b/redisconn/opts_internal_test.go new file mode 100644 index 0000000..e935cb0 --- /dev/null +++ b/redisconn/opts_internal_test.go @@ -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) +}