Skip to content

Commit 277648e

Browse files
authored
Merge pull request #38 from joomcode/feature/dial-timeout-above-io-timeout
Allow dial timeout to exceed IO timeout
2 parents 20c40ff + b487547 commit 277648e

2 files changed

Lines changed: 41 additions & 3 deletions

File tree

‎redisconn/conn.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ type Opts struct {
4646
// If IOTimeout < 0, then timeout is disabled
4747
IOTimeout time.Duration
4848
// DialTimeout is timeout for net.Dialer
49-
// If it is <= 0 or >= IOTimeout, then IOTimeout
49+
// If it is <= 0, then IOTimeout
5050
// If IOTimeout is disabled, then 5 seconds used (but without affect on ReconnectPause)
5151
DialTimeout time.Duration
5252
// 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
135135
conn.opts.IOTimeout = 0
136136
}
137137

138-
if conn.opts.DialTimeout <= 0 || conn.opts.DialTimeout > conn.opts.IOTimeout {
138+
if conn.opts.DialTimeout <= 0 {
139139
conn.opts.DialTimeout = conn.opts.IOTimeout
140140
}
141141

@@ -520,7 +520,7 @@ func (conn *Connection) dial() error {
520520
timeout := conn.opts.DialTimeout
521521
tlsEnabled := conn.opts.TLSEnabled
522522
tlsConfig := conn.opts.TLSConfig
523-
if timeout <= 0 || timeout > 5*time.Second {
523+
if timeout <= 0 {
524524
timeout = 5 * time.Second
525525
}
526526
if address[0] == '.' || address[0] == '/' {

‎redisconn/opts_internal_test.go‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package redisconn
2+
3+
import (
4+
"context"
5+
"testing"
6+
"time"
7+
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestDialTimeoutAboveIOTimeout(t *testing.T) {
12+
ctx, cancel := context.WithCancel(context.Background())
13+
defer cancel()
14+
15+
conn, err := Connect(ctx, "127.0.0.1:1", Opts{
16+
IOTimeout: 200 * time.Millisecond,
17+
DialTimeout: 3 * time.Second,
18+
AsyncDial: true,
19+
})
20+
require.NoError(t, err)
21+
defer conn.Close()
22+
23+
require.Equal(t, 3*time.Second, conn.opts.DialTimeout)
24+
}
25+
26+
func TestDialTimeoutDefaultsToIOTimeout(t *testing.T) {
27+
ctx, cancel := context.WithCancel(context.Background())
28+
defer cancel()
29+
30+
conn, err := Connect(ctx, "127.0.0.1:1", Opts{
31+
IOTimeout: 200 * time.Millisecond,
32+
AsyncDial: true,
33+
})
34+
require.NoError(t, err)
35+
defer conn.Close()
36+
37+
require.Equal(t, 200*time.Millisecond, conn.opts.DialTimeout)
38+
}

0 commit comments

Comments
 (0)