mirror of
https://github.com/status-im/consul.git
synced 2025-02-10 04:45:05 +00:00
* use defer to avoid tracking lock
* simplify control flow
This commit is contained in:
parent
218156aadb
commit
a030abdefc
@ -259,7 +259,16 @@ func (p *ConnPool) getNewConn(addr net.Addr, version int) (*Conn, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Wrap the connection
|
// Wrap the connection
|
||||||
c := &Conn{
|
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{
|
||||||
refCount: 1,
|
refCount: 1,
|
||||||
addr: addr,
|
addr: addr,
|
||||||
session: session,
|
session: session,
|
||||||
@ -268,18 +277,10 @@ func (p *ConnPool) getNewConn(addr net.Addr, version int) (*Conn, error) {
|
|||||||
version: version,
|
version: version,
|
||||||
pool: p,
|
pool: p,
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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.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
|
// clearConn is used to clear any cached connection, potentially in response to an erro
|
||||||
|
Loading…
x
Reference in New Issue
Block a user