From 7a012bbcc077bd2d67de2e37c81fa9c5ff06bbb5 Mon Sep 17 00:00:00 2001 From: Jieyi Long Date: Thu, 27 Jun 2019 15:01:33 -0700 Subject: [PATCH] Fixes for p2p connection error handling --- p2p/connection/connection.go | 11 +++++++++-- p2p/peer/peer_table.go | 1 + 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/p2p/connection/connection.go b/p2p/connection/connection.go index f3bb0a6f..95ddf21d 100644 --- a/p2p/connection/connection.go +++ b/p2p/connection/connection.go @@ -96,6 +96,8 @@ type ErrorHandler func(interface{}) // CreateConnection creates a Connection instance func CreateConnection(netconn net.Conn, config ConnectionConfig) *Connection { + logger.Debugf("Create connection, local: %v, remote: %v", netconn.LocalAddr(), netconn.RemoteAddr()) + channelCheckpoint := createDefaultChannel(common.ChannelIDCheckpoint) channelHeader := createDefaultChannel(common.ChannelIDHeader) channelBlock := createDefaultChannel(common.ChannelIDBlock) @@ -190,7 +192,11 @@ func (conn *Connection) CancelConnection() { // Stop is called whten the connection stops func (conn *Connection) Stop() { - conn.netconn.Close() + logger.Warnf("Stopping connection, local: %v, remote: %v", conn.GetNetconn().LocalAddr(), conn.GetNetconn().RemoteAddr()) + err := conn.netconn.Close() + if err != nil { + logger.Errorf("Failed to close connection: %v", err) + } conn.cancel() } @@ -304,7 +310,8 @@ func (conn *Connection) sendRoutine() { func (conn *Connection) sendPingSignal() error { if atomic.LoadUint32(&conn.pendingPings) >= conn.config.MaxPendingPings { - conn.onError(nil) + //conn.onError(nil) + conn.stopForError(nil) logger.Errorf("Peer not responding to ping %v", conn.netconn.RemoteAddr()) return fmt.Errorf("Peer not responding to ping %v", conn.netconn.RemoteAddr()) } diff --git a/p2p/peer/peer_table.go b/p2p/peer/peer_table.go index 004d280f..6b75e114 100644 --- a/p2p/peer/peer_table.go +++ b/p2p/peer/peer_table.go @@ -53,6 +53,7 @@ func (pt *PeerTable) AddPeer(peer *Peer) bool { for i, p := range pt.peers { if p.ID() == peer.ID() { p.Stop() + logger.Warnf("Stopping duplicated peer: %v", p.ID()) pt.peers[i] = peer break }