From fb1e5eac07dd48dafc12682521a7b48ccc7467eb Mon Sep 17 00:00:00 2001 From: Michel de Graaf Date: Tue, 11 Nov 2025 19:36:50 +0100 Subject: [PATCH] chore: tweak blocking peer connections --- src/actor/peer_actor.rs | 58 ++++++++++++++--------------------------- 1 file changed, 20 insertions(+), 38 deletions(-) diff --git a/src/actor/peer_actor.rs b/src/actor/peer_actor.rs index 0db50d4..634fc51 100644 --- a/src/actor/peer_actor.rs +++ b/src/actor/peer_actor.rs @@ -389,46 +389,28 @@ impl PeerActor { match socket_addr { Ok(addr) => { - if let Ok(stream) = TcpStream::connect(addr) { - if let Err(e) = stream.set_nonblocking(true) { - error!( - "[peer:{}] Failed to set non-blocking: {}", - username, e - ); - self.disconnect_with_error(e); - return false; - } - - stream.set_nodelay(true).ok(); - - self.stream = Some(stream); - self.connection_state = ConnectionState::Connecting { - since: Instant::now(), - }; - true - } else { - match TcpStream::connect(addr) { - Ok(stream) => { - if let Err(e) = stream.set_nonblocking(true) { - error!( - "[peer:{}] Failed to set non-blocking: {}", - username, e - ); - self.disconnect_with_error(e); - return false; - } - stream.set_nodelay(true).ok(); - self.stream = Some(stream); - self.connection_state = - ConnectionState::Connecting { - since: Instant::now(), - }; - true - } - Err(e) => { + // Use connect_timeout to prevent blocking the thread for too long + let timeout = Duration::from_secs(5); + match TcpStream::connect_timeout(&addr, timeout) { + Ok(stream) => { + if let Err(e) = stream.set_nonblocking(true) { + error!( + "[peer:{}] Failed to set non-blocking: {}", + username, e + ); self.disconnect_with_error(e); - false + return false; } + stream.set_nodelay(true).ok(); + self.stream = Some(stream); + self.connection_state = ConnectionState::Connecting { + since: Instant::now(), + }; + true + } + Err(e) => { + self.disconnect_with_error(e); + false } } }