Set cmd timeout when Block >= 0

This commit is contained in:
Vladimir Mihailenco 2018-09-13 09:14:52 +03:00
parent f7094544a5
commit 40dbb03d62
2 changed files with 11 additions and 8 deletions

View File

@ -8,13 +8,6 @@ import (
"github.com/go-redis/redis/internal" "github.com/go-redis/redis/internal"
) )
func readTimeout(timeout time.Duration) time.Duration {
if timeout == 0 {
return 0
}
return timeout + 10*time.Second
}
func usePrecise(dur time.Duration) bool { func usePrecise(dur time.Duration) bool {
return dur < time.Second || dur%time.Second != 0 return dur < time.Second || dur%time.Second != 0
} }
@ -1397,6 +1390,9 @@ func (c *cmdable) XRead(a *XReadArgs) *XStreamSliceCmd {
} }
cmd := NewXStreamSliceCmd(args...) cmd := NewXStreamSliceCmd(args...)
if a.Block >= 0 {
cmd.setReadTimeout(a.Block)
}
c.process(cmd) c.process(cmd)
return cmd return cmd
} }
@ -1455,6 +1451,9 @@ func (c *cmdable) XReadGroup(a *XReadGroupArgs) *XStreamSliceCmd {
} }
cmd := NewXStreamSliceCmd(args...) cmd := NewXStreamSliceCmd(args...)
if a.Block >= 0 {
cmd.setReadTimeout(a.Block)
}
c.process(cmd) c.process(cmd)
return cmd return cmd
} }

View File

@ -188,7 +188,11 @@ func (c *baseClient) retryBackoff(attempt int) time.Duration {
func (c *baseClient) cmdTimeout(cmd Cmder) time.Duration { func (c *baseClient) cmdTimeout(cmd Cmder) time.Duration {
if timeout := cmd.readTimeout(); timeout != nil { if timeout := cmd.readTimeout(); timeout != nil {
return readTimeout(*timeout) t := *timeout
if t == 0 {
return 0
}
return t + 10*time.Second
} }
return c.opt.ReadTimeout return c.opt.ReadTimeout
} }