From 308ebee45771b60fdefbbc8e7f0d69d0e233734a Mon Sep 17 00:00:00 2001 From: Vladimir Mihailenco Date: Sat, 28 Jan 2017 10:53:10 +0200 Subject: [PATCH] Fix defer order. --- internal/pool/pool_sticky.go | 39 ++++++++++++++++++++---------------- pipeline.go | 2 +- ring.go | 2 +- sentinel.go | 2 +- 4 files changed, 25 insertions(+), 20 deletions(-) diff --git a/internal/pool/pool_sticky.go b/internal/pool/pool_sticky.go index d25bf99a..9fb9971c 100644 --- a/internal/pool/pool_sticky.go +++ b/internal/pool/pool_sticky.go @@ -11,7 +11,7 @@ type StickyConnPool struct { cn *Conn closed bool - mx sync.Mutex + mu sync.Mutex } var _ Pooler = (*StickyConnPool)(nil) @@ -24,15 +24,15 @@ func NewStickyConnPool(pool *ConnPool, reusable bool) *StickyConnPool { } func (p *StickyConnPool) First() *Conn { - p.mx.Lock() + p.mu.Lock() cn := p.cn - p.mx.Unlock() + p.mu.Unlock() return cn } func (p *StickyConnPool) Get() (*Conn, bool, error) { - defer p.mx.Unlock() - p.mx.Lock() + p.mu.Lock() + defer p.mu.Unlock() if p.closed { return nil, false, ErrClosed @@ -56,8 +56,9 @@ func (p *StickyConnPool) putUpstream() (err error) { } func (p *StickyConnPool) Put(cn *Conn) error { - defer p.mx.Unlock() - p.mx.Lock() + p.mu.Lock() + defer p.mu.Unlock() + if p.closed { return ErrClosed } @@ -74,8 +75,9 @@ func (p *StickyConnPool) removeUpstream(reason error) error { } func (p *StickyConnPool) Remove(cn *Conn, reason error) error { - defer p.mx.Unlock() - p.mx.Lock() + p.mu.Lock() + defer p.mu.Unlock() + if p.closed { return nil } @@ -89,8 +91,9 @@ func (p *StickyConnPool) Remove(cn *Conn, reason error) error { } func (p *StickyConnPool) Len() int { - defer p.mx.Unlock() - p.mx.Lock() + p.mu.Lock() + defer p.mu.Unlock() + if p.cn == nil { return 0 } @@ -98,8 +101,9 @@ func (p *StickyConnPool) Len() int { } func (p *StickyConnPool) FreeLen() int { - defer p.mx.Unlock() - p.mx.Lock() + p.mu.Lock() + defer p.mu.Unlock() + if p.cn == nil { return 1 } @@ -111,8 +115,9 @@ func (p *StickyConnPool) Stats() *Stats { } func (p *StickyConnPool) Close() error { - defer p.mx.Unlock() - p.mx.Lock() + p.mu.Lock() + defer p.mu.Unlock() + if p.closed { return ErrClosed } @@ -130,8 +135,8 @@ func (p *StickyConnPool) Close() error { } func (p *StickyConnPool) Closed() bool { - p.mx.Lock() + p.mu.Lock() closed := p.closed - p.mx.Unlock() + p.mu.Unlock() return closed } diff --git a/pipeline.go b/pipeline.go index a0a00e20..13b29e1a 100644 --- a/pipeline.go +++ b/pipeline.go @@ -61,8 +61,8 @@ func (c *Pipeline) discard() error { // Exec always returns list of commands and error of the first failed // command if any. func (c *Pipeline) Exec() ([]Cmder, error) { - defer c.mu.Unlock() c.mu.Lock() + defer c.mu.Unlock() if c.closed { return nil, pool.ErrClosed diff --git a/ring.go b/ring.go index 4eb57c13..6e5f62ae 100644 --- a/ring.go +++ b/ring.go @@ -328,8 +328,8 @@ func (c *Ring) heartbeat() { // It is rare to Close a Ring, as the Ring is meant to be long-lived // and shared between many goroutines. func (c *Ring) Close() error { - defer c.mu.Unlock() c.mu.Lock() + defer c.mu.Unlock() if c.closed { return nil diff --git a/sentinel.go b/sentinel.go index 2a326477..77b892d5 100644 --- a/sentinel.go +++ b/sentinel.go @@ -162,8 +162,8 @@ func (d *sentinelFailover) Pool() *pool.ConnPool { } func (d *sentinelFailover) MasterAddr() (string, error) { - defer d.mu.Unlock() d.mu.Lock() + defer d.mu.Unlock() // Try last working sentinel. if d.sentinel != nil {