Merge pull request #1051 from go-redis/fix/context-min-nonzero-time

internal/pool: use min nonzero deadline
This commit is contained in:
Vladimir Mihailenco 2019-06-14 16:00:28 +03:00 committed by GitHub
commit d264eb8e81
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 25 additions and 15 deletions

View File

@ -1445,7 +1445,7 @@ func (c *ClusterClient) pubSub() *PubSub {
return nil, err return nil, err
} }
cn, err := node.Client.newConn() cn, err := node.Client.newConn(context.TODO())
if err != nil { if err != nil {
node = nil node = nil

View File

@ -82,18 +82,28 @@ func (cn *Conn) Close() error {
} }
func (cn *Conn) deadline(ctx context.Context, timeout time.Duration) time.Time { func (cn *Conn) deadline(ctx context.Context, timeout time.Duration) time.Time {
now := time.Now() tm := time.Now()
cn.SetUsedAt(now) cn.SetUsedAt(tm)
if timeout > 0 {
tm = tm.Add(timeout)
}
if ctx != nil { if ctx != nil {
tm, ok := ctx.Deadline() deadline, ok := ctx.Deadline()
if ok { if ok {
if timeout == 0 {
return deadline
}
if deadline.Before(tm) {
return deadline
}
return tm return tm
} }
} }
if timeout > 0 { if timeout > 0 {
return now.Add(timeout) return tm
} }
return noDeadline return noDeadline

View File

@ -34,7 +34,7 @@ type Stats struct {
} }
type Pooler interface { type Pooler interface {
NewConn() (*Conn, error) NewConn(context.Context) (*Conn, error)
CloseConn(*Conn) error CloseConn(*Conn) error
Get(context.Context) (*Conn, error) Get(context.Context) (*Conn, error)
@ -115,7 +115,7 @@ func (p *ConnPool) checkMinIdleConns() {
} }
func (p *ConnPool) addIdleConn() { func (p *ConnPool) addIdleConn() {
cn, err := p.newConn(nil, true) cn, err := p.newConn(context.TODO(), true)
if err != nil { if err != nil {
return return
} }
@ -126,8 +126,8 @@ func (p *ConnPool) addIdleConn() {
p.connsMu.Unlock() p.connsMu.Unlock()
} }
func (p *ConnPool) NewConn() (*Conn, error) { func (p *ConnPool) NewConn(ctx context.Context) (*Conn, error) {
return p._NewConn(nil, false) return p._NewConn(ctx, false)
} }
func (p *ConnPool) _NewConn(ctx context.Context, pooled bool) (*Conn, error) { func (p *ConnPool) _NewConn(ctx context.Context, pooled bool) (*Conn, error) {

View File

@ -14,7 +14,7 @@ func NewSingleConnPool(cn *Conn) *SingleConnPool {
} }
} }
func (p *SingleConnPool) NewConn() (*Conn, error) { func (p *SingleConnPool) NewConn(context.Context) (*Conn, error) {
panic("not implemented") panic("not implemented")
} }

View File

@ -23,7 +23,7 @@ func NewStickyConnPool(pool *ConnPool, reusable bool) *StickyConnPool {
} }
} }
func (p *StickyConnPool) NewConn() (*Conn, error) { func (p *StickyConnPool) NewConn(context.Context) (*Conn, error) {
panic("not implemented") panic("not implemented")
} }

View File

@ -139,8 +139,8 @@ func (c *baseClient) String() string {
return fmt.Sprintf("Redis<%s db:%d>", c.getAddr(), c.opt.DB) return fmt.Sprintf("Redis<%s db:%d>", c.getAddr(), c.opt.DB)
} }
func (c *baseClient) newConn() (*pool.Conn, error) { func (c *baseClient) newConn(ctx context.Context) (*pool.Conn, error) {
cn, err := c.connPool.NewConn() cn, err := c.connPool.NewConn(ctx)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -581,7 +581,7 @@ func (c *Client) pubSub() *PubSub {
opt: c.opt, opt: c.opt,
newConn: func(channels []string) (*pool.Conn, error) { newConn: func(channels []string) (*pool.Conn, error) {
return c.newConn() return c.newConn(context.TODO())
}, },
closeConn: c.connPool.CloseConn, closeConn: c.connPool.CloseConn,
} }

View File

@ -150,7 +150,7 @@ func (c *SentinelClient) pubSub() *PubSub {
opt: c.opt, opt: c.opt,
newConn: func(channels []string) (*pool.Conn, error) { newConn: func(channels []string) (*pool.Conn, error) {
return c.newConn() return c.newConn(context.TODO())
}, },
closeConn: c.connPool.CloseConn, closeConn: c.connPool.CloseConn,
} }