Merge pull request #482 from go-redis/fix/defer-order

Fix defer order.
This commit is contained in:
Vladimir Mihailenco 2017-01-28 11:10:00 +02:00 committed by GitHub
commit ba0b485159
4 changed files with 25 additions and 20 deletions

View File

@ -11,7 +11,7 @@ type StickyConnPool struct {
cn *Conn cn *Conn
closed bool closed bool
mx sync.Mutex mu sync.Mutex
} }
var _ Pooler = (*StickyConnPool)(nil) var _ Pooler = (*StickyConnPool)(nil)
@ -24,15 +24,15 @@ func NewStickyConnPool(pool *ConnPool, reusable bool) *StickyConnPool {
} }
func (p *StickyConnPool) First() *Conn { func (p *StickyConnPool) First() *Conn {
p.mx.Lock() p.mu.Lock()
cn := p.cn cn := p.cn
p.mx.Unlock() p.mu.Unlock()
return cn return cn
} }
func (p *StickyConnPool) Get() (*Conn, bool, error) { func (p *StickyConnPool) Get() (*Conn, bool, error) {
defer p.mx.Unlock() p.mu.Lock()
p.mx.Lock() defer p.mu.Unlock()
if p.closed { if p.closed {
return nil, false, ErrClosed return nil, false, ErrClosed
@ -56,8 +56,9 @@ func (p *StickyConnPool) putUpstream() (err error) {
} }
func (p *StickyConnPool) Put(cn *Conn) error { func (p *StickyConnPool) Put(cn *Conn) error {
defer p.mx.Unlock() p.mu.Lock()
p.mx.Lock() defer p.mu.Unlock()
if p.closed { if p.closed {
return ErrClosed return ErrClosed
} }
@ -74,8 +75,9 @@ func (p *StickyConnPool) removeUpstream(reason error) error {
} }
func (p *StickyConnPool) Remove(cn *Conn, reason error) error { func (p *StickyConnPool) Remove(cn *Conn, reason error) error {
defer p.mx.Unlock() p.mu.Lock()
p.mx.Lock() defer p.mu.Unlock()
if p.closed { if p.closed {
return nil return nil
} }
@ -89,8 +91,9 @@ func (p *StickyConnPool) Remove(cn *Conn, reason error) error {
} }
func (p *StickyConnPool) Len() int { func (p *StickyConnPool) Len() int {
defer p.mx.Unlock() p.mu.Lock()
p.mx.Lock() defer p.mu.Unlock()
if p.cn == nil { if p.cn == nil {
return 0 return 0
} }
@ -98,8 +101,9 @@ func (p *StickyConnPool) Len() int {
} }
func (p *StickyConnPool) FreeLen() int { func (p *StickyConnPool) FreeLen() int {
defer p.mx.Unlock() p.mu.Lock()
p.mx.Lock() defer p.mu.Unlock()
if p.cn == nil { if p.cn == nil {
return 1 return 1
} }
@ -111,8 +115,9 @@ func (p *StickyConnPool) Stats() *Stats {
} }
func (p *StickyConnPool) Close() error { func (p *StickyConnPool) Close() error {
defer p.mx.Unlock() p.mu.Lock()
p.mx.Lock() defer p.mu.Unlock()
if p.closed { if p.closed {
return ErrClosed return ErrClosed
} }
@ -130,8 +135,8 @@ func (p *StickyConnPool) Close() error {
} }
func (p *StickyConnPool) Closed() bool { func (p *StickyConnPool) Closed() bool {
p.mx.Lock() p.mu.Lock()
closed := p.closed closed := p.closed
p.mx.Unlock() p.mu.Unlock()
return closed return closed
} }

View File

@ -61,8 +61,8 @@ func (c *Pipeline) discard() error {
// Exec always returns list of commands and error of the first failed // Exec always returns list of commands and error of the first failed
// command if any. // command if any.
func (c *Pipeline) Exec() ([]Cmder, error) { func (c *Pipeline) Exec() ([]Cmder, error) {
defer c.mu.Unlock()
c.mu.Lock() c.mu.Lock()
defer c.mu.Unlock()
if c.closed { if c.closed {
return nil, pool.ErrClosed return nil, pool.ErrClosed

View File

@ -328,8 +328,8 @@ func (c *Ring) heartbeat() {
// It is rare to Close a Ring, as the Ring is meant to be long-lived // It is rare to Close a Ring, as the Ring is meant to be long-lived
// and shared between many goroutines. // and shared between many goroutines.
func (c *Ring) Close() error { func (c *Ring) Close() error {
defer c.mu.Unlock()
c.mu.Lock() c.mu.Lock()
defer c.mu.Unlock()
if c.closed { if c.closed {
return nil return nil

View File

@ -162,8 +162,8 @@ func (d *sentinelFailover) Pool() *pool.ConnPool {
} }
func (d *sentinelFailover) MasterAddr() (string, error) { func (d *sentinelFailover) MasterAddr() (string, error) {
defer d.mu.Unlock()
d.mu.Lock() d.mu.Lock()
defer d.mu.Unlock()
// Try last working sentinel. // Try last working sentinel.
if d.sentinel != nil { if d.sentinel != nil {