chore: tweak blocking peer connections
This commit is contained in:
+20
-38
@@ -389,46 +389,28 @@ impl PeerActor {
|
|||||||
|
|
||||||
match socket_addr {
|
match socket_addr {
|
||||||
Ok(addr) => {
|
Ok(addr) => {
|
||||||
if let Ok(stream) = TcpStream::connect(addr) {
|
// Use connect_timeout to prevent blocking the thread for too long
|
||||||
if let Err(e) = stream.set_nonblocking(true) {
|
let timeout = Duration::from_secs(5);
|
||||||
error!(
|
match TcpStream::connect_timeout(&addr, timeout) {
|
||||||
"[peer:{}] Failed to set non-blocking: {}",
|
Ok(stream) => {
|
||||||
username, e
|
if let Err(e) = stream.set_nonblocking(true) {
|
||||||
);
|
error!(
|
||||||
self.disconnect_with_error(e);
|
"[peer:{}] Failed to set non-blocking: {}",
|
||||||
return false;
|
username, e
|
||||||
}
|
);
|
||||||
|
|
||||||
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) => {
|
|
||||||
self.disconnect_with_error(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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user