Skip to content

Commit

Permalink
Merge pull request fedimint#6152 from dpc/24-10-10-debug-slow-apis
Browse files Browse the repository at this point in the history
fix(client): always adding retry delay, even on first connection
  • Loading branch information
dpc authored Oct 11, 2024
2 parents 3332cdc + d96498c commit 6ec7931
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ rand_chacha = { opt-level = 3 }
rand_core = { opt-level = 3 }
rand = { opt-level = 3 }
ring = { opt-level = 3 }
rustls = { opt-level = 3 }
secp256k1 = { opt-level = 3 }
secp256k1-sys = { opt-level = 3 }
subtle = { opt-level = 3 }
Expand Down
17 changes: 10 additions & 7 deletions fedimint-api-client/src/api/peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ where
) {
let mut connection_state_guard = connection_state.lock().await;

if connection_state_guard.last_connection_attempt_or.is_none() {
if connection_state_guard.last_connection_attempt.is_none() {
debug!(
target: LOG_CLIENT_NET_API,
peer_id = %peer_id,
Expand All @@ -169,7 +169,7 @@ where
struct FederationPeerClientConnectionState {
/// Last time a connection attempt was made, or `None` if no attempt has
/// been made yet.
last_connection_attempt_or: Option<SystemTime>,
last_connection_attempt: Option<SystemTime>,
connection_backoff: backoff_util::FibonacciBackoff,
}

Expand All @@ -179,7 +179,7 @@ impl FederationPeerClientConnectionState {

fn new() -> Self {
Self {
last_connection_attempt_or: None,
last_connection_attempt: None,
connection_backoff: backoff_util::custom_backoff(
Self::MIN_BACKOFF,
Self::MAX_BACKOFF,
Expand All @@ -193,12 +193,15 @@ impl FederationPeerClientConnectionState {
async fn wait(&mut self) {
let now_ts = now();
let desired_timeout = self.connection_backoff.next().unwrap_or(Self::MAX_BACKOFF);
let since_last_connect = match self.last_connection_attempt_or {
Some(last) => now_ts.duration_since(last).unwrap_or_default(),

let sleep_duration = match self.last_connection_attempt {
Some(last) => {
let since_last_connect = now_ts.duration_since(last).unwrap_or_default();
desired_timeout.saturating_sub(since_last_connect)
}
None => Duration::ZERO,
};

let sleep_duration = desired_timeout.saturating_sub(since_last_connect);
if Duration::ZERO < sleep_duration {
debug!(
target: LOG_CLIENT_NET_API,
Expand All @@ -207,7 +210,7 @@ impl FederationPeerClientConnectionState {
fedimint_core::runtime::sleep(sleep_duration).await;
}

self.last_connection_attempt_or = Some(now_ts);
self.last_connection_attempt = Some(now_ts);
}

fn reset(&mut self) {
Expand Down

0 comments on commit 6ec7931

Please sign in to comment.