chore: allow to disable timeouts

This commit is contained in:
Vladimir Mihailenco 2022-10-05 10:36:12 +03:00
parent 2123e08437
commit bac50ce2e9
2 changed files with 25 additions and 9 deletions

View File

@ -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 { 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 { if timeout >= 0 {
return err if err := cn.netConn.SetReadDeadline(cn.deadline(ctx, timeout)); err != nil {
return err
}
} }
return fn(cn.rd) 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( func (cn *Conn) WithWriter(
ctx context.Context, timeout time.Duration, fn func(wr *proto.Writer) error, ctx context.Context, timeout time.Duration, fn func(wr *proto.Writer) error,
) error { ) error {
if err := cn.netConn.SetWriteDeadline(cn.deadline(ctx, timeout)); err != nil { if timeout >= 0 {
return err if err := cn.netConn.SetWriteDeadline(cn.deadline(ctx, timeout)); err != nil {
return err
}
} }
if cn.bw.Buffered() > 0 { if cn.bw.Buffered() > 0 {

View File

@ -72,12 +72,16 @@ type Options struct {
// Default is 5 seconds. // Default is 5 seconds.
DialTimeout time.Duration DialTimeout time.Duration
// Timeout for socket reads. If reached, commands will fail // 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. // with a timeout instead of blocking. Supported values:
// Default is 3 seconds. // - `0` - default timeout (3 seconds).
// - `-1` - no timeout (block indefinitely).
// - `-2` - disables SetReadDeadline calls completely.
ReadTimeout time.Duration ReadTimeout time.Duration
// Timeout for socket writes. If reached, commands will fail // 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. // with a timeout instead of blocking. Supported values:
// Default is ReadTimeout. // - `0` - default timeout (3 seconds).
// - `-1` - no timeout (block indefinitely).
// - `-2` - disables SetWriteDeadline calls completely.
WriteTimeout time.Duration WriteTimeout time.Duration
// Type of connection pool. // Type of connection pool.
@ -144,19 +148,27 @@ func (opt *Options) init() {
opt.PoolSize = 10 * runtime.GOMAXPROCS(0) opt.PoolSize = 10 * runtime.GOMAXPROCS(0)
} }
switch opt.ReadTimeout { switch opt.ReadTimeout {
case -2:
opt.ReadTimeout = -1
case -1: case -1:
opt.ReadTimeout = 0 opt.ReadTimeout = 0
case 0: case 0:
opt.ReadTimeout = 3 * time.Second opt.ReadTimeout = 3 * time.Second
} }
switch opt.WriteTimeout { switch opt.WriteTimeout {
case -2:
opt.WriteTimeout = -1
case -1: case -1:
opt.WriteTimeout = 0 opt.WriteTimeout = 0
case 0: case 0:
opt.WriteTimeout = opt.ReadTimeout opt.WriteTimeout = opt.ReadTimeout
} }
if opt.PoolTimeout == 0 { 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 { if opt.ConnMaxIdleTime == 0 {
opt.ConnMaxIdleTime = 30 * time.Minute opt.ConnMaxIdleTime = 30 * time.Minute