Skip to content

Commit

Permalink
p2p: Cache inbound flag on Peer.isInbound to avoid a race
Browse files Browse the repository at this point in the history
  • Loading branch information
shazow committed Jun 7, 2018
1 parent 291fcfb commit 4e5f57a
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions p2p/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 4e5f57a

Please sign in to comment.