From f3fd07241810eb7eaac67fda5c307719c1e9d2c7 Mon Sep 17 00:00:00 2001 From: Armon Dadgar Date: Mon, 16 Feb 2015 14:04:47 -0800 Subject: [PATCH] Revert "* use defer to avoid tracking lock" This reverts commit a030abdefc54470394a2a44008e02f3b3d0510ec. This commit causes a connection to be leaked if there is a race with another concurrent RPC. --- consul/pool.go | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/consul/pool.go b/consul/pool.go index 048e2e6e2..523cfd525 100644 --- a/consul/pool.go +++ b/consul/pool.go @@ -259,28 +259,27 @@ func (p *ConnPool) getNewConn(addr net.Addr, version int) (*Conn, error) { } // Wrap the connection - var c *Conn + c := &Conn{ + refCount: 1, + addr: addr, + session: session, + clients: list.New(), + lastUsed: time.Now(), + version: version, + pool: p, + } // Track this connection, handle potential race condition p.Lock() - defer p.Unlock() - if existing := p.pool[addr.String()]; existing != nil { - c = existing + c.Close() + p.Unlock() + return existing, nil } else { - c = &Conn{ - refCount: 1, - addr: addr, - session: session, - clients: list.New(), - lastUsed: time.Now(), - version: version, - pool: p, - } p.pool[addr.String()] = c + p.Unlock() + return c, nil } - - return c, nil } // clearConn is used to clear any cached connection, potentially in response to an erro