eth: unregister peer only when handler exits (#22908)

This removes the error log message that says 

    Ethereum peer removal failed ... err="peer not registered"

The error happened because removePeer was called multiple
times: once to disconnect the peer, and another time when the
handler exited. With this change, removePeer now has the sole
purpose of disconnecting the peer. Unregistering happens exactly
once, when the handler exits.
This commit is contained in:
Felix Lange
2021-05-25 22:20:36 +02:00
committed by GitHub
parent 4d33de9b49
commit 836c647bdd
3 changed files with 60 additions and 30 deletions

View File

@ -115,7 +115,8 @@ type Peer struct {
disc chan DiscReason
// events receives message send / receive events if set
events *event.Feed
events *event.Feed
testPipe *MsgPipeRW // for testing
}
// NewPeer returns a peer for testing purposes.
@ -128,6 +129,15 @@ func NewPeer(id enode.ID, name string, caps []Cap) *Peer {
return peer
}
// NewPeerPipe creates a peer for testing purposes.
// The message pipe given as the last parameter is closed when
// Disconnect is called on the peer.
func NewPeerPipe(id enode.ID, name string, caps []Cap, pipe *MsgPipeRW) *Peer {
p := NewPeer(id, name, caps)
p.testPipe = pipe
return p
}
// ID returns the node's public key.
func (p *Peer) ID() enode.ID {
return p.rw.node.ID()
@ -185,6 +195,10 @@ func (p *Peer) LocalAddr() net.Addr {
// Disconnect terminates the peer connection with the given reason.
// It returns immediately and does not wait until the connection is closed.
func (p *Peer) Disconnect(reason DiscReason) {
if p.testPipe != nil {
p.testPipe.Close()
}
select {
case p.disc <- reason:
case <-p.closed: