Skip to content

Commit

Permalink
Fix sporadic pool timeouts with IdleTimeout != 0. Fixes redis#195.
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed Nov 27, 2015
1 parent 22ed1e8 commit fb44c89
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
1 change: 1 addition & 0 deletions cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ func (c *ClusterClient) process(cmd Cmder) {
pipe.Process(NewCmd("ASKING"))
pipe.Process(cmd)
_, _ = pipe.Exec()
pipe.Close()
ask = false
} else {
client.Process(cmd)
Expand Down
34 changes: 25 additions & 9 deletions pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,12 @@ func (p *connPool) First() *conn {
select {
case cn := <-p.freeConns:
if p.isIdle(cn) {
p.conns.Remove(cn)
continue
var err error
cn, err = p.replace(cn)
if err != nil {
log.Printf("redis: replace failed: %s", err)
continue
}
}
return cn
default:
Expand All @@ -181,8 +185,12 @@ func (p *connPool) wait() *conn {
select {
case cn := <-p.freeConns:
if p.isIdle(cn) {
p.Remove(cn)
continue
var err error
cn, err = p.replace(cn)
if err != nil {
log.Printf("redis: replace failed: %s", err)
continue
}
}
return cn
case <-deadline:
Expand Down Expand Up @@ -257,16 +265,24 @@ func (p *connPool) Put(cn *conn) error {
return nil
}

func (p *connPool) replace(cn *conn) (*conn, error) {
newcn, err := p.new()
if err != nil {
_ = p.conns.Remove(cn)
return nil, err
}
_ = p.conns.Replace(cn, newcn)
return newcn, nil
}

func (p *connPool) Remove(cn *conn) error {
// Replace existing connection with new one and unblock waiter.
newcn, err := p.new()
newcn, err := p.replace(cn)
if err != nil {
log.Printf("redis: new failed: %s", err)
return p.conns.Remove(cn)
return err
}
err = p.conns.Replace(cn, newcn)
p.freeConns <- newcn
return err
return nil
}

// Len returns total number of connections.
Expand Down

0 comments on commit fb44c89

Please sign in to comment.