Skip to content

Commit

Permalink
Fix 2 bugs in permanent connection retries
Browse files Browse the repository at this point in the history
  • Loading branch information
tiram88 committed Apr 25, 2023
1 parent 53024ef commit 3c86a0a
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions components/connectionmanager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ struct ConnectionRequest {
attempts: u32,
}

impl ConnectionRequest {
fn new(is_permanent: bool) -> Self {
Self { next_attempt: SystemTime::now(), is_permanent, attempts: 0 }
}
}

impl ConnectionManager {
pub fn new(
p2p_adaptor: Arc<kaspa_p2p_lib::Adaptor>,
Expand Down Expand Up @@ -96,10 +102,7 @@ impl ConnectionManager {

pub async fn add_connection_request(&self, address: SocketAddr, is_permanent: bool) {
// If the request already exists, it resets the attempts count and overrides the `is_permanent` setting.
self.connection_requests
.lock()
.await
.insert(address, ConnectionRequest { next_attempt: SystemTime::now(), is_permanent, attempts: 0 });
self.connection_requests.lock().await.insert(address, ConnectionRequest::new(is_permanent));
self.force_next_iteration.send(()).unwrap(); // We force the next iteration of the connection loop.
}

Expand All @@ -124,8 +127,8 @@ impl ConnectionManager {
if self.p2p_adaptor.connect_peer(address.to_string()).await.is_none() {
debug!("Failed connecting to peer request {}", address);
if request.is_permanent {
const MAX_RETRY_DURATION: Duration = Duration::from_secs(600);
let retry_duration = min(Duration::from_secs(30u64 * 2u64.pow(request.attempts)), MAX_RETRY_DURATION);
const MAX_ACCOUNTABLE_ATTEMPTS: u32 = 4;
let retry_duration = Duration::from_secs(30u64 * 2u64.pow(min(request.attempts, MAX_ACCOUNTABLE_ATTEMPTS)));
debug!("Will retry peer request {} in {}", address, DurationString::from(retry_duration));
new_requests.insert(
address,
Expand All @@ -138,7 +141,7 @@ impl ConnectionManager {
}
} else if request.is_permanent {
// Permanent requests are kept forever
new_requests.insert(address, request);
new_requests.insert(address, ConnectionRequest::new(true));
}
} else {
new_requests.insert(address, request);
Expand Down

0 comments on commit 3c86a0a

Please sign in to comment.