From 0ff0a2a8a0530b185a6094fff7094dced137a862 Mon Sep 17 00:00:00 2001 From: Arik Sosman Date: Tue, 5 Sep 2023 21:28:15 -0700 Subject: [PATCH] Don't special-case initial connection attempt. --- src/tracking.rs | 53 ++++++++++++++++++++++++++----------------------- 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/src/tracking.rs b/src/tracking.rs index ee428e0..d25b60a 100644 --- a/src/tracking.rs +++ b/src/tracking.rs @@ -11,7 +11,7 @@ use lightning; use lightning::ln::peer_handler::{ ErroringMessageHandler, IgnoringMessageHandler, MessageHandler, PeerManager, }; -use lightning::{log_error, log_info, log_warn}; +use lightning::{log_info, log_warn}; use lightning::routing::gossip::NetworkGraph; use lightning::sign::KeysManager; use lightning::util::logger::Logger; @@ -165,31 +165,34 @@ pub(crate) async fn download_gossip(pe } async fn connect_peer(current_peer: (PublicKey, SocketAddr), peer_manager: GossipPeerManager, logger: L) -> bool where L::Target: Logger { - log_info!(logger, "Connecting to peer {}@{}...", current_peer.0.to_hex(), current_peer.1.to_string()); - let connection = lightning_net_tokio::connect_outbound( - Arc::clone(&peer_manager), - current_peer.0, - current_peer.1, - ).await; - if let Some(disconnection_future) = connection { - log_info!(logger, "Connected to peer {}@{}!", current_peer.0.to_hex(), current_peer.1.to_string()); - tokio::spawn(async move { - disconnection_future.await; - loop { - log_warn!(logger, "Reconnecting to peer {}@{}...", current_peer.0.to_hex(), current_peer.1.to_string()); - if let Some(disconnection_future) = lightning_net_tokio::connect_outbound( - Arc::clone(&peer_manager), - current_peer.0, - current_peer.1, - ).await { - disconnection_future.await; + // we seek to find out if the first connection attempt was successful + let (sender, mut receiver) = mpsc::channel::(1); + tokio::spawn(async move { + log_info!(logger, "Connecting to peer {}@{}...", current_peer.0.to_hex(), current_peer.1.to_string()); + let mut is_first_iteration = true; + loop { + if let Some(disconnection_future) = lightning_net_tokio::connect_outbound( + Arc::clone(&peer_manager), + current_peer.0, + current_peer.1, + ).await { + log_info!(logger, "Connected to peer {}@{}!", current_peer.0.to_hex(), current_peer.1.to_string()); + if is_first_iteration { + sender.send(true).await.unwrap(); } + disconnection_future.await; + log_warn!(logger, "Disconnected from peer {}@{}...", current_peer.0.to_hex(), current_peer.1.to_string()); tokio::time::sleep(Duration::from_secs(10)).await; + log_warn!(logger, "Reconnecting to peer {}@{}...", current_peer.0.to_hex(), current_peer.1.to_string()); + } else { + if is_first_iteration { + sender.send(false).await.unwrap(); + } } - }); - true - } else { - log_error!(logger, "Failed to connect to peer {}@{}", current_peer.0.to_hex(), current_peer.1.to_string()); - false - } + is_first_iteration = false; + } + }); + + let success = receiver.recv().await.unwrap(); + success } -- 2.39.5