+ /// Disconnects all currently-connected peers. This is useful on platforms where there may be
+ /// an indication that TCP sockets have stalled even if we weren't around to time them out
+ /// using regular ping/pongs.
+ pub fn disconnect_all_peers(&self) {
+ let mut peers_lock = self.peers.lock().unwrap();
+ let peers = &mut *peers_lock;
+ for (mut descriptor, peer) in peers.peers.drain() {
+ if let Some(node_id) = peer.their_node_id {
+ log_trace!(self.logger, "Disconnecting peer with id {} due to client request to disconnect all peers", node_id);
+ peers.node_id_to_descriptor.remove(&node_id);
+ self.message_handler.chan_handler.peer_disconnected(&node_id, false);
+ }
+ descriptor.disconnect_socket();
+ }
+ debug_assert!(peers.node_id_to_descriptor.is_empty());
+ }
+
+ /// This is called when we're blocked on sending additional gossip messages until we receive a
+ /// pong. If we aren't waiting on a pong, we take this opportunity to send a ping (setting
+ /// `awaiting_pong_tick_intervals` to a special flag value to indicate this).
+ fn maybe_send_extra_ping(&self, peer: &mut Peer) {
+ if peer.awaiting_pong_tick_intervals == 0 {
+ peer.awaiting_pong_tick_intervals = -1;
+ let ping = msgs::Ping {
+ ponglen: 0,
+ byteslen: 64,
+ };
+ self.enqueue_message(peer, &ping);
+ }
+ }
+
+ /// Send pings to each peer and disconnect those which did not respond to the last round of
+ /// pings.
+ ///
+ /// This may be called on any timescale you want, however, roughly once every five to ten
+ /// seconds is preferred. The call rate determines both how often we send a ping to our peers
+ /// and how much time they have to respond before we disconnect them.