Skip to content

Commit

Permalink
Add 1 more second to reconnect
Browse files Browse the repository at this point in the history
Also, code is cleaner.
  • Loading branch information
2e3s committed Jun 11, 2024
1 parent 214ff1c commit 1f7ed75
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions watchers/src/report_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,22 @@ impl ReportClient {
Fut: Future<Output = Result<T, E>>,
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(
Expand Down

0 comments on commit 1f7ed75

Please sign in to comment.