mirror of
https://github.com/btcsuite/btcd.git
synced 2024-11-19 01:40:07 +01:00
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:
parent
6c00d07910
commit
d0f0a2ac02
33
server.go
33
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 {
|
||||
|
Loading…
Reference in New Issue
Block a user