mirror of https://github.com/go-redis/redis.git
Merge pull request #1405 from fishy/atomic-value-last-dial-error
Use atomic.Value instead of lock for ConnPool.lastDialError
This commit is contained in:
commit
f2645d373d
|
@ -62,13 +62,16 @@ type Options struct {
|
||||||
IdleCheckFrequency time.Duration
|
IdleCheckFrequency time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type lastDialErrorWrap struct {
|
||||||
|
err error
|
||||||
|
}
|
||||||
|
|
||||||
type ConnPool struct {
|
type ConnPool struct {
|
||||||
opt *Options
|
opt *Options
|
||||||
|
|
||||||
dialErrorsNum uint32 // atomic
|
dialErrorsNum uint32 // atomic
|
||||||
|
|
||||||
lastDialErrorMu sync.RWMutex
|
lastDialError atomic.Value
|
||||||
lastDialError error
|
|
||||||
|
|
||||||
queue chan struct{}
|
queue chan struct{}
|
||||||
|
|
||||||
|
@ -207,16 +210,15 @@ func (p *ConnPool) tryDial() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *ConnPool) setLastDialError(err error) {
|
func (p *ConnPool) setLastDialError(err error) {
|
||||||
p.lastDialErrorMu.Lock()
|
p.lastDialError.Store(&lastDialErrorWrap{err: err})
|
||||||
p.lastDialError = err
|
|
||||||
p.lastDialErrorMu.Unlock()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *ConnPool) getLastDialError() error {
|
func (p *ConnPool) getLastDialError() error {
|
||||||
p.lastDialErrorMu.RLock()
|
err, _ := p.lastDialError.Load().(*lastDialErrorWrap)
|
||||||
err := p.lastDialError
|
if err != nil {
|
||||||
p.lastDialErrorMu.RUnlock()
|
return err.err
|
||||||
return err
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get returns existed connection from the pool or creates a new one.
|
// Get returns existed connection from the pool or creates a new one.
|
||||||
|
|
Loading…
Reference in New Issue