Skip to content

Commit

Permalink
Merge pull request redis#654 from go-redis/fix/pool-panic
Browse files Browse the repository at this point in the history
Fix pool panics
  • Loading branch information
vmihailenco authored Oct 12, 2017
2 parents 9c20773 + dac1820 commit 3524815
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions internal/pool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 3524815

Please sign in to comment.