| 397 | } |
| 398 | |
| 399 | func (sw *Switch) stopAndRemovePeer(peer Peer, reason interface{}) { |
| 400 | sw.transport.Cleanup(peer) |
| 401 | if err := peer.Stop(); err != nil { |
| 402 | sw.Logger.Error("error while stopping peer", "error", err) // TODO: should return error to be handled accordingly |
| 403 | } |
| 404 | |
| 405 | for _, reactor := range sw.reactors { |
| 406 | reactor.RemovePeer(peer, reason) |
| 407 | } |
| 408 | |
| 409 | // Removing a peer should go last to avoid a situation where a peer |
| 410 | // reconnect to our node and the switch calls InitPeer before |
| 411 | // RemovePeer is finished. |
| 412 | // https://github.com/DeAI-Artist/Linkis/issues/3338 |
| 413 | if sw.peers.Remove(peer) { |
| 414 | sw.metrics.Peers.Add(float64(-1)) |
| 415 | } else { |
| 416 | // Removal of the peer has failed. The function above sets a flag within the peer to mark this. |
| 417 | // We keep this message here as information to the developer. |
| 418 | sw.Logger.Debug("error on peer removal", ",", "peer", peer.ID()) |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | // reconnectToPeer tries to reconnect to the addr, first repeatedly |
| 423 | // with a fixed interval, then with exponential backoff. |