From d0f0a2ac02596ff312987fa1fe75972e5bf9a227 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Sun, 22 Nov 2015 02:00:43 -0600 Subject: [PATCH] server: Improve handling of disconnected peers. When the peer code was refactored, the lists of peers were converted to maps however the code which runs when a peer disconnects still iterates them like a slice. This is no longer necessary since they are maps which means the peer can simply be looked up by its ID. Also, the old code was comparing the map entry and the peer being removed by their pointers which could lead to potentially not properly finding the peer. This issue is also resolved by this change since it looks up the peer by its ID. --- server.go | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/server.go b/server.go index ced9387a3..2346d4de3 100644 --- a/server.go +++ b/server.go @@ -1117,27 +1117,26 @@ func (s *server) handleDonePeerMsg(state *peerState, sp *serverPeer) { } else { list = state.outboundPeers } - for id, e := range list { - if e == sp { - // Issue an asynchronous reconnect if the peer was a - // persistent outbound connection. - if !sp.Inbound() && sp.persistent && atomic.LoadInt32(&s.shutdown) == 0 { - // Retry peer - sp = s.newOutboundPeer(sp.Addr(), sp.persistent) - if sp != nil { - go s.retryConn(sp, connectionRetryInterval/2) - } - list[id] = sp - return + if _, ok := list[sp.ID()]; ok { + // Issue an asynchronous reconnect if the peer was a + // persistent outbound connection. + if !sp.Inbound() && sp.persistent && atomic.LoadInt32(&s.shutdown) == 0 { + // Retry peer + sp = s.newOutboundPeer(sp.Addr(), sp.persistent) + if sp != nil { + go s.retryConn(sp, connectionRetryInterval/2) } - if !sp.Inbound() && sp.VersionKnown() { - state.outboundGroups[addrmgr.GroupKey(sp.NA())]-- - } - delete(list, id) - srvrLog.Debugf("Removed peer %s", sp) + list[sp.ID()] = sp return } + if !sp.Inbound() && sp.VersionKnown() { + state.outboundGroups[addrmgr.GroupKey(sp.NA())]-- + } + delete(list, sp.ID()) + srvrLog.Debugf("Removed peer %s", sp) + return } + // Update the address' last seen time if the peer has acknowledged // our version and has sent us its version as well. if sp.VerAckReceived() && sp.VersionKnown() && sp.NA() != nil {