network: handle errors and connection close more correctly

This makes writer side handle errors properly and fixes communication between
reader and writer goroutine to always correctly unregister the peer. This is
especially important for the case where error occurs before handshake
completes as in this case we don't even have goroutine in startProtocol()
running.
This commit is contained in:
Roman Khimov 2019-09-13 15:36:53 +03:00
parent 76c7cff67f
commit d3bb8ddf8f
3 changed files with 45 additions and 26 deletions

View file

@ -136,7 +136,12 @@ func (p *TCPPeer) Done() chan error {
// Disconnect will fill the peer's done channel with the given error.
func (p *TCPPeer) Disconnect(err error) {
p.conn.Close()
p.done <- err
select {
case p.done <- err:
// one message to the queue
default:
// the other side may already be gone, it's OK
}
}
// Version implements the Peer interface.