From 1f7ed75a0b79b30240e38bcee5cd98844dead0b9 Mon Sep 17 00:00:00 2001 From: Demmie <2e3s19@gmail.com> Date: Tue, 11 Jun 2024 14:27:10 -0400 Subject: [PATCH] Add 1 more second to reconnect Also, code is cleaner. --- watchers/src/report_client.rs | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/watchers/src/report_client.rs b/watchers/src/report_client.rs index 811f5d8..e80ce53 100644 --- a/watchers/src/report_client.rs +++ b/watchers/src/report_client.rs @@ -39,24 +39,22 @@ impl ReportClient { Fut: Future>, E: std::error::Error + Send + Sync + 'static, { - let mut interval = tokio::time::interval(tokio::time::Duration::from_secs(1)); - let mut attempts = 0; - loop { + for (attempt, &secs) in [1, 2].iter().enumerate() { match f().await { Ok(val) => return Ok(val), Err(e) - if attempts < 3 - && e.to_string() - .contains("tcp connect error: Connection refused") => + if e.to_string() + .contains("tcp connect error: Connection refused") => { - warn!("Failed to connect, retrying: {}", e); + warn!("Failed to connect on attempt #{attempt}, retrying: {}", e); - attempts += 1; - interval.tick().await; + tokio::time::sleep(tokio::time::Duration::from_secs(secs)).await; } Err(e) => return Err(e), } } + + f().await } pub async fn ping(