From bac50ce2e953769cb93f1a4e044253c744afa3cc Mon Sep 17 00:00:00 2001 From: Vladimir Mihailenco Date: Wed, 5 Oct 2022 10:36:12 +0300 Subject: [PATCH] chore: allow to disable timeouts --- internal/pool/conn.go | 12 ++++++++---- options.go | 22 +++++++++++++++++----- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/internal/pool/conn.go b/internal/pool/conn.go index 5b5079a5..f160a9fa 100644 --- a/internal/pool/conn.go +++ b/internal/pool/conn.go @@ -64,8 +64,10 @@ func (cn *Conn) RemoteAddr() net.Addr { } func (cn *Conn) WithReader(ctx context.Context, timeout time.Duration, fn func(rd *proto.Reader) error) error { - if err := cn.netConn.SetReadDeadline(cn.deadline(ctx, timeout)); err != nil { - return err + if timeout >= 0 { + if err := cn.netConn.SetReadDeadline(cn.deadline(ctx, timeout)); err != nil { + return err + } } return fn(cn.rd) } @@ -73,8 +75,10 @@ func (cn *Conn) WithReader(ctx context.Context, timeout time.Duration, fn func(r func (cn *Conn) WithWriter( ctx context.Context, timeout time.Duration, fn func(wr *proto.Writer) error, ) error { - if err := cn.netConn.SetWriteDeadline(cn.deadline(ctx, timeout)); err != nil { - return err + if timeout >= 0 { + if err := cn.netConn.SetWriteDeadline(cn.deadline(ctx, timeout)); err != nil { + return err + } } if cn.bw.Buffered() > 0 { diff --git a/options.go b/options.go index 0c2cb1d1..bc8d3cb8 100644 --- a/options.go +++ b/options.go @@ -72,12 +72,16 @@ type Options struct { // Default is 5 seconds. DialTimeout time.Duration // Timeout for socket reads. If reached, commands will fail - // with a timeout instead of blocking. Use value -1 for no timeout and 0 for default. - // Default is 3 seconds. + // with a timeout instead of blocking. Supported values: + // - `0` - default timeout (3 seconds). + // - `-1` - no timeout (block indefinitely). + // - `-2` - disables SetReadDeadline calls completely. ReadTimeout time.Duration // Timeout for socket writes. If reached, commands will fail - // with a timeout instead of blocking. Use value -1 for no timeout and 0 for default. - // Default is ReadTimeout. + // with a timeout instead of blocking. Supported values: + // - `0` - default timeout (3 seconds). + // - `-1` - no timeout (block indefinitely). + // - `-2` - disables SetWriteDeadline calls completely. WriteTimeout time.Duration // Type of connection pool. @@ -144,19 +148,27 @@ func (opt *Options) init() { opt.PoolSize = 10 * runtime.GOMAXPROCS(0) } switch opt.ReadTimeout { + case -2: + opt.ReadTimeout = -1 case -1: opt.ReadTimeout = 0 case 0: opt.ReadTimeout = 3 * time.Second } switch opt.WriteTimeout { + case -2: + opt.WriteTimeout = -1 case -1: opt.WriteTimeout = 0 case 0: opt.WriteTimeout = opt.ReadTimeout } if opt.PoolTimeout == 0 { - opt.PoolTimeout = opt.ReadTimeout + time.Second + if opt.ReadTimeout > 0 { + opt.PoolTimeout = opt.ReadTimeout + time.Second + } else { + opt.PoolTimeout = 30 * time.Second + } } if opt.ConnMaxIdleTime == 0 { opt.ConnMaxIdleTime = 30 * time.Minute