From 1744695aae05dae99f5384e5c0e4e746be2afe47 Mon Sep 17 00:00:00 2001 From: Tony Giorgio Date: Fri, 5 Jan 2024 15:34:50 -0600 Subject: [PATCH 1/3] Less aggresive peer reconnection --- mutiny-core/src/node.rs | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/mutiny-core/src/node.rs b/mutiny-core/src/node.rs index 8e2798703..17538a73c 100644 --- a/mutiny-core/src/node.rs +++ b/mutiny-core/src/node.rs @@ -83,7 +83,7 @@ use std::{ }, }; -const INITIAL_RECONNECTION_DELAY: u64 = 5; +const INITIAL_RECONNECTION_DELAY: u64 = 10; const MAX_RECONNECTION_DELAY: u64 = 60; pub(crate) type BumpTxEventHandler = BumpTransactionEventHandler< @@ -2025,8 +2025,28 @@ async fn start_reconnection_handling( // hashMap to store backoff times for each pubkey let mut backoff_times = HashMap::new(); + // Only begin this process after 30s of running + for _ in 0..30 { + if stop.load(Ordering::Relaxed) { + log_debug!( + connect_logger, + "stopping connection component and disconnecting peers for node: {}", + node_pubkey.to_hex(), + ); + connect_peer_man.disconnect_all_peers(); + stop_component(&stopped_components); + log_debug!( + connect_logger, + "stopped connection component and disconnected peers for node: {}", + node_pubkey.to_hex(), + ); + return; + } + sleep(1_000).await; + } + loop { - for _ in 0..5 { + for _ in 0..10 { if stop.load(Ordering::Relaxed) { log_debug!( connect_logger, From 0539b933706a48b7d02b3a513244e327af2380e6 Mon Sep 17 00:00:00 2001 From: Tony Giorgio Date: Tue, 9 Jan 2024 13:38:33 -0600 Subject: [PATCH 2/3] Merge the initial connection threads --- mutiny-core/src/node.rs | 40 +++++++++++++++++----------------------- 1 file changed, 17 insertions(+), 23 deletions(-) diff --git a/mutiny-core/src/node.rs b/mutiny-core/src/node.rs index 17538a73c..cd5a882fe 100644 --- a/mutiny-core/src/node.rs +++ b/mutiny-core/src/node.rs @@ -2014,14 +2014,8 @@ async fn start_reconnection_handling( log_error!(proxy_logger, "could not save connection to lsp: {e}"); } }; - }); - // keep trying to connect each lightning peer if they get disconnected - let connect_peer_man = peer_man.clone(); - let connect_fee_estimator = fee_estimator.clone(); - let connect_logger = logger.clone(); - let connect_storage = storage.clone(); - utils::spawn(async move { + // keep trying to connect each lightning peer if they get disconnected // hashMap to store backoff times for each pubkey let mut backoff_times = HashMap::new(); @@ -2029,14 +2023,14 @@ async fn start_reconnection_handling( for _ in 0..30 { if stop.load(Ordering::Relaxed) { log_debug!( - connect_logger, + proxy_logger, "stopping connection component and disconnecting peers for node: {}", node_pubkey.to_hex(), ); - connect_peer_man.disconnect_all_peers(); + peer_man_proxy.disconnect_all_peers(); stop_component(&stopped_components); log_debug!( - connect_logger, + proxy_logger, "stopped connection component and disconnected peers for node: {}", node_pubkey.to_hex(), ); @@ -2049,14 +2043,14 @@ async fn start_reconnection_handling( for _ in 0..10 { if stop.load(Ordering::Relaxed) { log_debug!( - connect_logger, + proxy_logger, "stopping connection component and disconnecting peers for node: {}", node_pubkey.to_hex(), ); - connect_peer_man.disconnect_all_peers(); + peer_man_proxy.disconnect_all_peers(); stop_component(&stopped_components); log_debug!( - connect_logger, + proxy_logger, "stopped connection component and disconnected peers for node: {}", node_pubkey.to_hex(), ); @@ -2065,8 +2059,8 @@ async fn start_reconnection_handling( sleep(1_000).await; } - let peer_connections = get_all_peers(&connect_storage).unwrap_or_default(); - let current_connections = connect_peer_man.get_peer_node_ids(); + let peer_connections = get_all_peers(&storage_copy).unwrap_or_default(); + let current_connections = peer_man_proxy.get_peer_node_ids(); let not_connected: Vec<(NodeId, String)> = peer_connections .into_iter() @@ -2098,11 +2092,11 @@ async fn start_reconnection_handling( // Update the last attempt time backoff_entry.1 = now; - log_trace!(connect_logger, "going to auto connect to peer: {pubkey}"); + log_trace!(proxy_logger, "going to auto connect to peer: {pubkey}"); let peer_connection_info = match PubkeyConnectionInfo::new(&conn_str) { Ok(p) => p, Err(e) => { - log_error!(connect_logger, "could not parse connection info: {e}"); + log_error!(proxy_logger, "could not parse connection info: {e}"); continue; } }; @@ -2111,21 +2105,21 @@ async fn start_reconnection_handling( #[cfg(target_arch = "wasm32")] &websocket_proxy_addr, &peer_connection_info, - &connect_storage, - connect_logger.clone(), - connect_peer_man.clone(), - connect_fee_estimator.clone(), + &storage_copy, + proxy_logger.clone(), + peer_man_proxy.clone(), + proxy_fee_estimator.clone(), stop.clone(), ) .await; match connect_res { Ok(_) => { - log_trace!(connect_logger, "auto connected peer: {pubkey}"); + log_trace!(proxy_logger, "auto connected peer: {pubkey}"); // reset backoff time to initial value if connection is successful backoff_entry.0 = INITIAL_RECONNECTION_DELAY; } Err(e) => { - log_warn!(connect_logger, "could not auto connect peer: {e}"); + log_warn!(proxy_logger, "could not auto connect peer: {e}"); // double the backoff time if connection fails, but do not exceed max backoff_entry.0 = (backoff_entry.0 * 2).min(MAX_RECONNECTION_DELAY); } From 83f62be2ce2fd12b05e7219c67913c5139bf9e61 Mon Sep 17 00:00:00 2001 From: Tony Giorgio Date: Tue, 9 Jan 2024 14:08:14 -0600 Subject: [PATCH 3/3] Attempt connections to non-lsp peers before loop --- mutiny-core/src/node.rs | 53 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/mutiny-core/src/node.rs b/mutiny-core/src/node.rs index cd5a882fe..65d633384 100644 --- a/mutiny-core/src/node.rs +++ b/mutiny-core/src/node.rs @@ -1981,7 +1981,8 @@ async fn start_reconnection_handling( let stop_copy = stop.clone(); utils::spawn(async move { // Now try to connect to the client's LSP - if let Some(lsp) = lsp_client_copy { + // This is here in case the LSP client node info has not saved to storage yet + if let Some(lsp) = lsp_client_copy.as_ref() { let node_id = NodeId::from_pubkey(&lsp.get_lsp_pubkey()); let connect_res = connect_peer_if_necessary( @@ -2015,6 +2016,54 @@ async fn start_reconnection_handling( } }; + // Now try to connect to other nodes the client might have, skipping the LSP if necessary + let stored_peers = get_all_peers(&storage_copy).unwrap_or_default(); + let lsp_node_id = lsp_client_copy.map(|lsp| NodeId::from_pubkey(&lsp.get_lsp_pubkey())); + let initial_peers: Vec<(NodeId, String)> = stored_peers + .into_iter() + .filter(|(_, d)| { + d.connection_string.is_some() && d.nodes.binary_search(&uuid.to_string()).is_ok() + }) + .map(|(n, d)| (n, d.connection_string.unwrap())) + .filter(|(n, _)| lsp_node_id != Some(*n)) + .collect(); + for (pubkey, conn_str) in initial_peers.into_iter() { + log_trace!( + proxy_logger, + "starting initial connection to peer: {pubkey}" + ); + let peer_connection_info = match PubkeyConnectionInfo::new(&conn_str) { + Ok(p) => p, + Err(e) => { + log_error!(proxy_logger, "could not parse connection info: {e}"); + continue; + } + }; + + let connect_res = connect_peer_if_necessary( + #[cfg(target_arch = "wasm32")] + &websocket_proxy_addr, + &peer_connection_info, + &storage_copy, + proxy_logger.clone(), + peer_man_proxy.clone(), + proxy_fee_estimator.clone(), + stop.clone(), + ) + .await; + match connect_res { + Ok(_) => { + log_trace!(proxy_logger, "initial connection to peer: {pubkey}"); + } + Err(e) => { + log_warn!( + proxy_logger, + "could not start initial connection to peer: {e}" + ); + } + } + } + // keep trying to connect each lightning peer if they get disconnected // hashMap to store backoff times for each pubkey let mut backoff_times = HashMap::new(); @@ -2040,7 +2089,7 @@ async fn start_reconnection_handling( } loop { - for _ in 0..10 { + for _ in 0..INITIAL_RECONNECTION_DELAY { if stop.load(Ordering::Relaxed) { log_debug!( proxy_logger,