Merge pull request #523 from go-redis/fix/timer

Fix wrong usage of timer Reset(), which could cause service frozen during master switch.
This commit is contained in:
Vladimir Mihailenco 2017-03-04 12:02:13 +03:00 committed by GitHub
commit e90826e4c6
1 changed files with 11 additions and 7 deletions

View File

@ -19,7 +19,9 @@ var (
var timers = sync.Pool{ var timers = sync.Pool{
New: func() interface{} { New: func() interface{} {
return time.NewTimer(0) t := time.NewTimer(time.Hour)
t.Stop()
return t
}, },
} }
@ -95,12 +97,13 @@ func (p *ConnPool) NewConn() (*Conn, error) {
func (p *ConnPool) PopFree() *Conn { func (p *ConnPool) PopFree() *Conn {
timer := timers.Get().(*time.Timer) timer := timers.Get().(*time.Timer)
if !timer.Reset(p.poolTimeout) { timer.Reset(p.poolTimeout)
<-timer.C
}
select { select {
case p.queue <- struct{}{}: case p.queue <- struct{}{}:
if !timer.Stop() {
<-timer.C
}
timers.Put(timer) timers.Put(timer)
case <-timer.C: case <-timer.C:
timers.Put(timer) timers.Put(timer)
@ -138,12 +141,13 @@ func (p *ConnPool) Get() (*Conn, bool, error) {
atomic.AddUint32(&p.stats.Requests, 1) atomic.AddUint32(&p.stats.Requests, 1)
timer := timers.Get().(*time.Timer) timer := timers.Get().(*time.Timer)
if !timer.Reset(p.poolTimeout) { timer.Reset(p.poolTimeout)
<-timer.C
}
select { select {
case p.queue <- struct{}{}: case p.queue <- struct{}{}:
if !timer.Stop() {
<-timer.C
}
timers.Put(timer) timers.Put(timer)
case <-timer.C: case <-timer.C:
timers.Put(timer) timers.Put(timer)