1
0
mirror of https://github.com/redis/go-redis.git synced 2025-08-01 16:06:54 +03:00

Fix pool panics

This commit is contained in:
Dimitrij Denissenko
2017-10-11 16:03:55 +01:00
parent 9c20773cb2
commit dac1820e47

View File

@ -60,8 +60,10 @@ type Options struct {
type ConnPool struct { type ConnPool struct {
opt *Options opt *Options
dialErrorsNum uint32 // atomic dialErrorsNum uint32 // atomic
_lastDialError atomic.Value
lastDialError error
lastDialErrorMu sync.RWMutex
queue chan struct{} queue chan struct{}
@ -98,7 +100,7 @@ func (p *ConnPool) NewConn() (*Conn, error) {
} }
if atomic.LoadUint32(&p.dialErrorsNum) >= uint32(p.opt.PoolSize) { if atomic.LoadUint32(&p.dialErrorsNum) >= uint32(p.opt.PoolSize) {
return nil, p.lastDialError() return nil, p.getLastDialError()
} }
netConn, err := p.opt.Dialer() netConn, err := p.opt.Dialer()
@ -138,11 +140,16 @@ func (p *ConnPool) tryDial() {
} }
func (p *ConnPool) setLastDialError(err error) { func (p *ConnPool) setLastDialError(err error) {
p._lastDialError.Store(err) p.lastDialErrorMu.Lock()
p.lastDialError = err
p.lastDialErrorMu.Unlock()
} }
func (p *ConnPool) lastDialError() error { func (p *ConnPool) getLastDialError() error {
return p._lastDialError.Load().(error) p.lastDialErrorMu.RLock()
err := p.lastDialError
p.lastDialErrorMu.RUnlock()
return err
} }
// Get returns existed connection from the pool or creates a new one. // Get returns existed connection from the pool or creates a new one.