Skip to content

Commit

Permalink
Fixes for p2p connection error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jieyilong committed Jun 27, 2019
1 parent e88b25e commit 7a012bb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
11 changes: 9 additions & 2 deletions p2p/connection/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
}

Expand Down Expand Up @@ -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())
}
Expand Down
1 change: 1 addition & 0 deletions p2p/peer/peer_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 7a012bb

Please sign in to comment.