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.
This commit is contained in:
Armon Dadgar 2015-02-16 14:04:47 -08:00
parent 5634061a9f
commit f3fd072418
1 changed files with 14 additions and 15 deletions

View File

@ -259,16 +259,7 @@ func (p *ConnPool) getNewConn(addr net.Addr, version int) (*Conn, error) {
}
// Wrap the connection
var c *Conn
// Track this connection, handle potential race condition
p.Lock()
defer p.Unlock()
if existing := p.pool[addr.String()]; existing != nil {
c = existing
} else {
c = &Conn{
c := &Conn{
refCount: 1,
addr: addr,
session: session,
@ -277,11 +268,19 @@ func (p *ConnPool) getNewConn(addr net.Addr, version int) (*Conn, error) {
version: version,
pool: p,
}
p.pool[addr.String()] = c
}
// Track this connection, handle potential race condition
p.Lock()
if existing := p.pool[addr.String()]; existing != nil {
c.Close()
p.Unlock()
return existing, nil
} else {
p.pool[addr.String()] = c
p.Unlock()
return c, nil
}
}
// clearConn is used to clear any cached connection, potentially in response to an erro
func (p *ConnPool) clearConn(conn *Conn) {