From 0a965c5d706923057cd6849622b74d9a428aa398 Mon Sep 17 00:00:00 2001 From: Vladimir Mihailenco Date: Thu, 29 Jun 2017 12:54:49 +0300 Subject: [PATCH] pool: add fast path --- internal/pool/pool.go | 48 +++++++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/internal/pool/pool.go b/internal/pool/pool.go index da8337a..88a252a 100644 --- a/internal/pool/pool.go +++ b/internal/pool/pool.go @@ -106,19 +106,23 @@ func (p *ConnPool) NewConn() (*Conn, error) { } func (p *ConnPool) PopFree() *Conn { - timer := timers.Get().(*time.Timer) - timer.Reset(p.poolTimeout) - select { case p.queue <- struct{}{}: - if !timer.Stop() { - <-timer.C + default: + timer := timers.Get().(*time.Timer) + timer.Reset(p.poolTimeout) + + select { + case p.queue <- struct{}{}: + if !timer.Stop() { + <-timer.C + } + timers.Put(timer) + case <-timer.C: + timers.Put(timer) + atomic.AddUint32(&p.stats.Timeouts, 1) + return nil } - timers.Put(timer) - case <-timer.C: - timers.Put(timer) - atomic.AddUint32(&p.stats.Timeouts, 1) - return nil } p.freeConnsMu.Lock() @@ -150,19 +154,23 @@ func (p *ConnPool) Get() (*Conn, bool, error) { atomic.AddUint32(&p.stats.Requests, 1) - timer := timers.Get().(*time.Timer) - timer.Reset(p.poolTimeout) - select { case p.queue <- struct{}{}: - if !timer.Stop() { - <-timer.C + default: + timer := timers.Get().(*time.Timer) + timer.Reset(p.poolTimeout) + + select { + case p.queue <- struct{}{}: + if !timer.Stop() { + <-timer.C + } + timers.Put(timer) + case <-timer.C: + timers.Put(timer) + atomic.AddUint32(&p.stats.Timeouts, 1) + return nil, false, ErrPoolTimeout } - timers.Put(timer) - case <-timer.C: - timers.Put(timer) - atomic.AddUint32(&p.stats.Timeouts, 1) - return nil, false, ErrPoolTimeout } for {