From bd0c7684c84e23742041baa90c0614aa4425be80 Mon Sep 17 00:00:00 2001 From: lunar-mining Date: Sun, 14 Jan 2024 16:00:54 +0100 Subject: [PATCH] outbound_session: replace downgrade_host() with `rejected` vector downgrade_host() is costly (reads through many vectors) and redundant because it repeats the behavior that should be handled by the greylist refinery. what we need is a way to avoid instantly reconnecting to hosts that we have just failed to connect to. this commit introduces a simple fix- we write to a vector called `rejected` and avoid trying to reconnect to its entries once a connection has failed. however, this is not ideal as it means we will never connect to that host- see TODO. --- src/net/p2p.rs | 4 +--- src/net/session/outbound_session.rs | 30 ++++++++++++++--------------- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/src/net/p2p.rs b/src/net/p2p.rs index 9e5f680d2..451b72065 100644 --- a/src/net/p2p.rs +++ b/src/net/p2p.rs @@ -228,9 +228,7 @@ impl P2p { /// Check whether we're connected to a given address pub async fn exists(&self, addr: &Url) -> bool { - //self.channels.lock().await.contains_key(addr) - //true - false + self.channels.lock().await.contains_key(addr) } /// Add a channel to the set of connected channels diff --git a/src/net/session/outbound_session.rs b/src/net/session/outbound_session.rs index c650c7467..bcdfb5d6b 100644 --- a/src/net/session/outbound_session.rs +++ b/src/net/session/outbound_session.rs @@ -229,7 +229,6 @@ impl Slot { // * address is already pending a connection let addr = hosts.check_address_with_lock(self.p2p(), addrs).await; return addr - //return None } // We first try to make connections to the addresses on our anchor list. We then find some @@ -238,6 +237,7 @@ impl Slot { async fn run(self: Arc) { let hosts = self.p2p().hosts(); let slot_count = self.p2p().settings().outbound_connections; + let mut rejected = vec![]; loop { // Activate the slot @@ -262,6 +262,7 @@ impl Slot { self.session().wakeup_peer_discovery(); // Wait to be woken up by peer discovery self.wakeup_self.wait().await; + continue } @@ -279,11 +280,10 @@ impl Slot { self.session().wakeup_peer_discovery(); // Wait to be woken up by peer discovery self.wakeup_self.wait().await; + continue }; - debug!(target: "deadlock", "Got addrs: {}, slot {} node {}", addr.0, self.slot, self.p2p().settings().node_id); - let host = addr.0; let slot = self.slot; @@ -298,6 +298,10 @@ impl Slot { addr: host.clone(), }); + if rejected.contains(&host) { + continue + } + let (addr, channel) = match self.try_connect(host.clone()).await { Ok(connect_info) => connect_info, Err(err) => { @@ -307,20 +311,18 @@ impl Slot { slot, err, self.p2p().settings().node_id ); - debug!( - target: "deadlock", - "connection failed: {}, slot {} node {}, channel {}", - err, slot, self.p2p().settings().node_id, host.clone() - ); - dnetev!(self, OutboundSlotDisconnected, { slot, err: err.to_string() }); - self.channel_id.store(0, Ordering::Relaxed); + // Add to the rejected list to avoid immediately reconnecting. + // TODO: Once it's been added to this list it will never be connected to + // in the lifespan of Outbound Session. Refactor this so that it gets put + // in a temporary quarantine and is freed up to try again later. + rejected.push(host.clone()); - sleep(1).await; + self.channel_id.store(0, Ordering::Relaxed); continue } }; @@ -337,6 +339,8 @@ impl Slot { channel_id: channel.info.id }); + // At this point we've managed to connect. + let stop_sub = channel.subscribe_stop().await.expect("Channel should not be stopped"); // Setup new channel if let Err(err) = self.setup_channel(host.clone(), channel.clone()).await { @@ -387,10 +391,6 @@ impl Slot { self.slot, addr, e ); - //// At this point we've failed to connect. - //// If the host is in the anchorlist or whitelist, downgrade it to greylist. - //self.p2p().hosts().downgrade_host(&addr).await?; - // Remove connection from pending self.p2p().remove_pending(&addr).await;