p2p: Cache inbound flag on Peer.isInbound to avoid a race

This commit is contained in:
Andrey Petrov 2018-06-07 10:42:40 -04:00
parent 399aa710d5
commit dcca66bce8

@ -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
}