use anyhow::anyhow; use lemmy_utils::LemmyError; use std::future::Future; use thiserror::Error; #[derive(Clone, Debug, Error)] #[error("Error sending request, {0}")] struct SendError(pub String); #[derive(Clone, Debug, Error)] #[error("Error receiving response, {0}")] pub struct RecvError(pub String); pub async fn retry(f: F) -> Result where F: Fn() -> Fut, Fut: Future>, { retry_custom(|| async { Ok((f)().await) }).await } async fn retry_custom(f: F) -> Result where F: Fn() -> Fut, Fut: Future, LemmyError>>, { let mut response = Err(anyhow!("connect timeout").into()); for _ in 0u8..3 { match (f)().await? { Ok(t) => return Ok(t), Err(e) => { if e.is_timeout() { response = Err(SendError(e.to_string()).into()); continue; } return Err(SendError(e.to_string()).into()); } } } response }