peer: Remove potential race when calling Connect.

This commit is contained in:
Jonathan Gillham 2016-02-10 14:23:36 +00:00
parent 7de7bddba9
commit 5cbd1f85bf

View file

@ -1953,7 +1953,7 @@ func (p *Peer) QueueInventory(invVect *wire.InvVect) {
// the peer is already connected will have no effect.
func (p *Peer) Connect(conn net.Conn) error {
// Already connected?
if atomic.LoadInt32(&p.connected) != 0 {
if !atomic.CompareAndSwapInt32(&p.connected, 0, 1) {
return nil
}
@ -1963,7 +1963,6 @@ func (p *Peer) Connect(conn net.Conn) error {
p.conn = conn
p.timeConnected = time.Now()
atomic.AddInt32(&p.connected, 1)
return p.start()
}