From 216ec11a0e8175294eb2f983ef73bbe94be6d7ec Mon Sep 17 00:00:00 2001 From: yuekui Date: Fri, 3 Mar 2017 15:45:40 -0800 Subject: [PATCH] Fix wrong usage of timer Reset(), which could cause service frozen during master switch. --- internal/pool/pool.go | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/internal/pool/pool.go b/internal/pool/pool.go index 4033e58d..c97875f4 100644 --- a/internal/pool/pool.go +++ b/internal/pool/pool.go @@ -19,7 +19,9 @@ var ( var timers = sync.Pool{ 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 { timer := timers.Get().(*time.Timer) - if !timer.Reset(p.poolTimeout) { - <-timer.C - } + timer.Reset(p.poolTimeout) select { case p.queue <- struct{}{}: + if !timer.Stop() { + <-timer.C + } timers.Put(timer) case <-timer.C: timers.Put(timer) @@ -138,12 +141,13 @@ func (p *ConnPool) Get() (*Conn, bool, error) { atomic.AddUint32(&p.stats.Requests, 1) timer := timers.Get().(*time.Timer) - if !timer.Reset(p.poolTimeout) { - <-timer.C - } + timer.Reset(p.poolTimeout) select { case p.queue <- struct{}{}: + if !timer.Stop() { + <-timer.C + } timers.Put(timer) case <-timer.C: timers.Put(timer)