mirror of
https://github.com/LemmyNet/lemmy.git
synced 2024-11-29 07:41:20 +00:00
fa4825b524
* Removing a few expects from production and test code. - Fixes #5192 * Using if let filter for admin emails. * Fixing unused error. * Adding expect_used = deny to clippy lints. * Update src/lib.rs Co-authored-by: Nutomic <me@nutomic.com> * Update crates/utils/src/settings/structs.rs Co-authored-by: Nutomic <me@nutomic.com> * Update crates/utils/src/settings/mod.rs Co-authored-by: Nutomic <me@nutomic.com> * Some more cleanup. * Fix clippy --------- Co-authored-by: Nutomic <me@nutomic.com>
38 lines
1 KiB
Rust
38 lines
1 KiB
Rust
use std::future::Future;
|
|
|
|
#[tracing::instrument(skip_all)]
|
|
pub async fn retry<F, Fut, T>(f: F) -> Result<T, reqwest_middleware::Error>
|
|
where
|
|
F: Fn() -> Fut,
|
|
Fut: Future<Output = Result<T, reqwest_middleware::Error>>,
|
|
{
|
|
retry_custom(|| async { Ok((f)().await) }).await
|
|
}
|
|
|
|
#[tracing::instrument(skip_all)]
|
|
#[allow(clippy::expect_used)]
|
|
async fn retry_custom<F, Fut, T>(f: F) -> Result<T, reqwest_middleware::Error>
|
|
where
|
|
F: Fn() -> Fut,
|
|
Fut: Future<Output = Result<Result<T, reqwest_middleware::Error>, reqwest_middleware::Error>>,
|
|
{
|
|
let mut response: Option<Result<T, reqwest_middleware::Error>> = None;
|
|
|
|
for _ in 0u8..3 {
|
|
match (f)().await? {
|
|
Ok(t) => return Ok(t),
|
|
Err(reqwest_middleware::Error::Reqwest(e)) => {
|
|
if e.is_timeout() {
|
|
response = Some(Err(reqwest_middleware::Error::Reqwest(e)));
|
|
continue;
|
|
}
|
|
return Err(reqwest_middleware::Error::Reqwest(e));
|
|
}
|
|
Err(otherwise) => {
|
|
return Err(otherwise);
|
|
}
|
|
}
|
|
}
|
|
|
|
response.expect("retry http request")
|
|
}
|