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.
This commit is contained in:
Dave Collins 2015-11-22 02:00:43 -06:00
parent 6c00d07910
commit d0f0a2ac02

View File

@ -1117,8 +1117,7 @@ func (s *server) handleDonePeerMsg(state *peerState, sp *serverPeer) {
} else {
list = state.outboundPeers
}
for id, e := range list {
if e == sp {
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 {
@ -1127,17 +1126,17 @@ func (s *server) handleDonePeerMsg(state *peerState, sp *serverPeer) {
if sp != nil {
go s.retryConn(sp, connectionRetryInterval/2)
}
list[id] = sp
list[sp.ID()] = sp
return
}
if !sp.Inbound() && sp.VersionKnown() {
state.outboundGroups[addrmgr.GroupKey(sp.NA())]--
}
delete(list, id)
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 {