aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrey Petrov <andrey.petrov@shazow.net>2018-06-07 22:42:40 +0800
committerAndrey Petrov <andrey.petrov@shazow.net>2018-06-22 00:22:47 +0800
commitdcca66bce8ec79bcf0e06c32f57d0011f8d9fa93 (patch)
treef1baf018b3cfd106719ea530043f4c746122a33c
parent399aa710d514561be571dc180aa4afe9fcc2138d (diff)
downloadgo-tangerine-dcca66bce8ec79bcf0e06c32f57d0011f8d9fa93.tar
go-tangerine-dcca66bce8ec79bcf0e06c32f57d0011f8d9fa93.tar.gz
go-tangerine-dcca66bce8ec79bcf0e06c32f57d0011f8d9fa93.tar.bz2
go-tangerine-dcca66bce8ec79bcf0e06c32f57d0011f8d9fa93.tar.lz
go-tangerine-dcca66bce8ec79bcf0e06c32f57d0011f8d9fa93.tar.xz
go-tangerine-dcca66bce8ec79bcf0e06c32f57d0011f8d9fa93.tar.zst
go-tangerine-dcca66bce8ec79bcf0e06c32f57d0011f8d9fa93.zip
p2p: Cache inbound flag on Peer.isInbound to avoid a race
-rw-r--r--p2p/peer.go26
1 files changed, 14 insertions, 12 deletions
diff --git a/p2p/peer.go b/p2p/peer.go
index c3907349f..ff8602602 100644
--- a/p2p/peer.go
+++ b/p2p/peer.go
@@ -95,10 +95,11 @@ type PeerEvent struct {
// Peer represents a connected remote node.
type Peer struct {
- rw *conn
- running map[string]*protoRW
- log log.Logger
- created mclock.AbsTime
+ rw *conn
+ isInbound bool // Cached from rw.flags to avoid a race condition
+ running map[string]*protoRW
+ log log.Logger
+ created mclock.AbsTime
wg sync.WaitGroup
protoErr chan error
@@ -160,19 +161,20 @@ func (p *Peer) String() string {
// Inbound returns true if the peer is an inbound connection
func (p *Peer) Inbound() bool {
- return p.rw.flags&inboundConn != 0
+ return p.isInbound
}
func newPeer(conn *conn, protocols []Protocol) *Peer {
protomap := matchProtocols(protocols, conn.caps, conn)
p := &Peer{
- rw: conn,
- running: protomap,
- created: mclock.Now(),
- disc: make(chan DiscReason),
- protoErr: make(chan error, len(protomap)+1), // protocols + pingLoop
- closed: make(chan struct{}),
- log: log.New("id", conn.id, "conn", conn.flags),
+ rw: conn,
+ isInbound: conn.is(inboundConn),
+ running: protomap,
+ created: mclock.Now(),
+ disc: make(chan DiscReason),
+ protoErr: make(chan error, len(protomap)+1), // protocols + pingLoop
+ closed: make(chan struct{}),
+ log: log.New("id", conn.id, "conn", conn.flags),
}
return p
}