Skip to content

Commit 90625a3

Browse files
authored
Merge pull request #2176 from j178/fix-read-timeout
fix(internal/pool): call SetDeadline even if timeout is zero
2 parents fda4fee + bac50ce commit 90625a3

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

internal/pool/conn.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func (cn *Conn) RemoteAddr() net.Addr {
6464
}
6565

6666
func (cn *Conn) WithReader(ctx context.Context, timeout time.Duration, fn func(rd *proto.Reader) error) error {
67-
if timeout != 0 {
67+
if timeout >= 0 {
6868
if err := cn.netConn.SetReadDeadline(cn.deadline(ctx, timeout)); err != nil {
6969
return err
7070
}
@@ -75,7 +75,7 @@ func (cn *Conn) WithReader(ctx context.Context, timeout time.Duration, fn func(r
7575
func (cn *Conn) WithWriter(
7676
ctx context.Context, timeout time.Duration, fn func(wr *proto.Writer) error,
7777
) error {
78-
if timeout != 0 {
78+
if timeout >= 0 {
7979
if err := cn.netConn.SetWriteDeadline(cn.deadline(ctx, timeout)); err != nil {
8080
return err
8181
}

options.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,16 @@ type Options struct {
7272
// Default is 5 seconds.
7373
DialTimeout time.Duration
7474
// Timeout for socket reads. If reached, commands will fail
75-
// with a timeout instead of blocking. Use value -1 for no timeout and 0 for default.
76-
// Default is 3 seconds.
75+
// with a timeout instead of blocking. Supported values:
76+
// - `0` - default timeout (3 seconds).
77+
// - `-1` - no timeout (block indefinitely).
78+
// - `-2` - disables SetReadDeadline calls completely.
7779
ReadTimeout time.Duration
7880
// Timeout for socket writes. If reached, commands will fail
79-
// with a timeout instead of blocking.
80-
// Default is ReadTimeout.
81+
// with a timeout instead of blocking. Supported values:
82+
// - `0` - default timeout (3 seconds).
83+
// - `-1` - no timeout (block indefinitely).
84+
// - `-2` - disables SetWriteDeadline calls completely.
8185
WriteTimeout time.Duration
8286

8387
// Type of connection pool.
@@ -144,19 +148,27 @@ func (opt *Options) init() {
144148
opt.PoolSize = 10 * runtime.GOMAXPROCS(0)
145149
}
146150
switch opt.ReadTimeout {
151+
case -2:
152+
opt.ReadTimeout = -1
147153
case -1:
148154
opt.ReadTimeout = 0
149155
case 0:
150156
opt.ReadTimeout = 3 * time.Second
151157
}
152158
switch opt.WriteTimeout {
159+
case -2:
160+
opt.WriteTimeout = -1
153161
case -1:
154162
opt.WriteTimeout = 0
155163
case 0:
156164
opt.WriteTimeout = opt.ReadTimeout
157165
}
158166
if opt.PoolTimeout == 0 {
159-
opt.PoolTimeout = opt.ReadTimeout + time.Second
167+
if opt.ReadTimeout > 0 {
168+
opt.PoolTimeout = opt.ReadTimeout + time.Second
169+
} else {
170+
opt.PoolTimeout = 30 * time.Second
171+
}
160172
}
161173
if opt.ConnMaxIdleTime == 0 {
162174
opt.ConnMaxIdleTime = 30 * time.Minute

0 commit comments

Comments
 (0)