From 5e1d3b4e11e0cfb4689530e17081bea9edbfa806 Mon Sep 17 00:00:00 2001 From: shadowWalker Date: Mon, 18 Nov 2024 15:34:31 +0700 Subject: [PATCH] fix: dup outgoing conn (#21) * log ts for debug secure handshake * fix: duplicate retry outgoing connection every tick * chore: fix clippy --- src/lib.rs | 1 + src/peer.rs | 20 ++++++++++++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 2011a34..80d63e2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -260,6 +260,7 @@ impl P2pNetwork { Ok(P2pNetworkEvent::Continue) } InternalEvent::PeerConnectError(conn, peer, err) => { + self.neighbours.remove(&conn); log::error!("[P2pNetwork] connection {conn} outgoing: {peer:?} error {err}"); Ok(P2pNetworkEvent::Continue) } diff --git a/src/peer.rs b/src/peer.rs index b96ec90..588cc8e 100644 --- a/src/peer.rs +++ b/src/peer.rs @@ -33,6 +33,7 @@ enum PeerConnectionControl { pub struct PeerConnection { conn_id: ConnectionId, peer_id: Option, + is_connected: bool, } impl PeerConnection { @@ -58,7 +59,11 @@ impl PeerConnection { Err(err) => internal_tx.send(InternalEvent::PeerConnectError(conn_id, None, err.into())).await.expect("should send to main"), } }); - Self { conn_id, peer_id: None } + Self { + conn_id, + peer_id: None, + is_connected: false, + } } pub fn new_connecting(secure: Arc, local_id: PeerId, to_peer: PeerId, connecting: Connecting, internal_tx: Sender, ctx: SharedCtx) -> Self { @@ -87,7 +92,11 @@ impl PeerConnection { .expect("should send to main"), } }); - Self { conn_id, peer_id: None } + Self { + conn_id, + peer_id: Some(to_peer), + is_connected: false, + } } pub fn conn_id(&self) -> ConnectionId { @@ -99,11 +108,14 @@ impl PeerConnection { } pub fn set_connected(&mut self, peer_id: PeerId) { - self.peer_id = Some(peer_id); + if self.peer_id.is_none() { + self.peer_id = Some(peer_id); + } + self.is_connected = true } pub fn is_connected(&self) -> bool { - self.peer_id.is_some() + self.is_connected } }