From dac1820e476ae285d7b7fe069268a7f5d1f5a549 Mon Sep 17 00:00:00 2001 From: Dimitrij Denissenko Date: Wed, 11 Oct 2017 16:03:55 +0100 Subject: [PATCH] Fix pool panics --- internal/pool/pool.go | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/internal/pool/pool.go b/internal/pool/pool.go index 836ec104..ae81905e 100644 --- a/internal/pool/pool.go +++ b/internal/pool/pool.go @@ -60,8 +60,10 @@ type Options struct { type ConnPool struct { opt *Options - dialErrorsNum uint32 // atomic - _lastDialError atomic.Value + dialErrorsNum uint32 // atomic + + lastDialError error + lastDialErrorMu sync.RWMutex queue chan struct{} @@ -98,7 +100,7 @@ func (p *ConnPool) NewConn() (*Conn, error) { } if atomic.LoadUint32(&p.dialErrorsNum) >= uint32(p.opt.PoolSize) { - return nil, p.lastDialError() + return nil, p.getLastDialError() } netConn, err := p.opt.Dialer() @@ -138,11 +140,16 @@ func (p *ConnPool) tryDial() { } func (p *ConnPool) setLastDialError(err error) { - p._lastDialError.Store(err) + p.lastDialErrorMu.Lock() + p.lastDialError = err + p.lastDialErrorMu.Unlock() } -func (p *ConnPool) lastDialError() error { - return p._lastDialError.Load().(error) +func (p *ConnPool) getLastDialError() error { + p.lastDialErrorMu.RLock() + err := p.lastDialError + p.lastDialErrorMu.RUnlock() + return err } // Get returns existed connection from the pool or creates a new one.