]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Add `PeerManager::disconnect_all_peers` to avoid complexity in BP
authorMatt Corallo <git@bluematt.me>
Tue, 26 Oct 2021 02:03:02 +0000 (02:03 +0000)
committerMatt Corallo <git@bluematt.me>
Tue, 26 Oct 2021 02:04:34 +0000 (02:04 +0000)
In the coming commits simply calling `timer_tick_occurred` will no
longer disconnect all peers, so its helpful to have a utility
method.

lightning-background-processor/src/lib.rs
lightning/src/ln/peer_handler.rs

index e38a4a975b2a8ff4a9c750081c9d43166ffd21af..902bef6af0a60433503f7d0d88de27ac2ace4395 100644 (file)
@@ -14,8 +14,7 @@ use lightning::chain::chainmonitor::{ChainMonitor, Persist};
 use lightning::chain::keysinterface::{Sign, KeysInterface};
 use lightning::ln::channelmanager::ChannelManager;
 use lightning::ln::msgs::{ChannelMessageHandler, RoutingMessageHandler};
-use lightning::ln::peer_handler::{PeerManager, SocketDescriptor};
-use lightning::ln::peer_handler::CustomMessageHandler;
+use lightning::ln::peer_handler::{CustomMessageHandler, PeerManager, SocketDescriptor};
 use lightning::routing::network_graph::NetGraphMsgHandler;
 use lightning::util::events::{Event, EventHandler, EventsProvider};
 use lightning::util::logger::Logger;
@@ -236,8 +235,7 @@ impl BackgroundProcessor {
                                        // timer, we should have disconnected all sockets by now (and they're probably
                                        // dead anyway), so disconnect them by calling `timer_tick_occurred()` twice.
                                        log_trace!(logger, "Awoke after more than double our ping timer, disconnecting peers.");
-                                       peer_manager.timer_tick_occurred();
-                                       peer_manager.timer_tick_occurred();
+                                       peer_manager.disconnect_all_peers();
                                        last_ping_call = Instant::now();
                                } else if last_ping_call.elapsed().as_secs() > PING_TIMER {
                                        log_trace!(logger, "Calling PeerManager's timer_tick_occurred");
index 1815d4a350cc0289100d207b9fa2a98029397b1d..74a6676d3e26a5939f02b4ac4e51d930e0b8d9cf 100644 (file)
@@ -1431,6 +1431,23 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref, CMH: Deref> P
                }
        }
 
+       /// 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());
+       }
+
        /// Send pings to each peer and disconnect those which did not respond to the last round of
        /// pings.
        ///