Skip to content

Commit

Permalink
Merge pull request redis#482 from go-redis/fix/defer-order
Browse files Browse the repository at this point in the history
Fix defer order.
  • Loading branch information
vmihailenco authored Jan 28, 2017
2 parents 9cd4965 + 308ebee commit ba0b485
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 20 deletions.
39 changes: 22 additions & 17 deletions internal/pool/pool_sticky.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type StickyConnPool struct {

cn *Conn
closed bool
mx sync.Mutex
mu sync.Mutex
}

var _ Pooler = (*StickyConnPool)(nil)
Expand All @@ -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
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -89,17 +91,19 @@ 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
}
return 1
}

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
}
Expand All @@ -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
}
Expand All @@ -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
}
2 changes: 1 addition & 1 deletion pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion ring.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion sentinel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit ba0b485

Please sign in to comment.