Add 1 more second to reconnect

Also, code is cleaner.
This commit is contained in:
Demmie 2024-06-11 14:27:10 -04:00
parent 214ff1c06e
commit 1f7ed75a0b
No known key found for this signature in database
GPG Key ID: B06DAA3D432C6E9A

View File

@ -39,24 +39,22 @@ impl ReportClient {
Fut: Future<Output = Result<T, E>>, Fut: Future<Output = Result<T, E>>,
E: std::error::Error + Send + Sync + 'static, E: std::error::Error + Send + Sync + 'static,
{ {
let mut interval = tokio::time::interval(tokio::time::Duration::from_secs(1)); for (attempt, &secs) in [1, 2].iter().enumerate() {
let mut attempts = 0;
loop {
match f().await { match f().await {
Ok(val) => return Ok(val), Ok(val) => return Ok(val),
Err(e) Err(e)
if attempts < 3 if e.to_string()
&& e.to_string() .contains("tcp connect error: Connection refused") =>
.contains("tcp connect error: Connection refused") =>
{ {
warn!("Failed to connect, retrying: {}", e); warn!("Failed to connect on attempt #{attempt}, retrying: {}", e);
attempts += 1; tokio::time::sleep(tokio::time::Duration::from_secs(secs)).await;
interval.tick().await;
} }
Err(e) => return Err(e), Err(e) => return Err(e),
} }
} }
f().await
} }
pub async fn ping( pub async fn ping(