-
Notifications
You must be signed in to change notification settings - Fork 396
Expand file tree
/
Copy pathretry.rs
More file actions
62 lines (56 loc) · 1.87 KB
/
retry.rs
File metadata and controls
62 lines (56 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
use backon::ExponentialBuilder;
use backon::Retryable;
use std::{future::Future, time::Duration};
#[derive(Debug)]
pub enum RetryError<E> {
Transient(E),
Permanent(E),
}
impl<E: std::fmt::Display> std::fmt::Display for RetryError<E> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
RetryError::Transient(e) => write!(f, "{}", e),
RetryError::Permanent(e) => write!(f, "{}", e),
}
}
}
impl<E> RetryError<E> {
pub fn inner(self) -> E {
match self {
RetryError::Transient(e) => e,
RetryError::Permanent(e) => e,
}
}
}
impl<E: std::fmt::Display> std::error::Error for RetryError<E> where E: std::fmt::Debug {}
/// Supports retries only on async functions. See: https://docs.rs/backon/latest/backon/#retry-an-async-function
/// Runs with `jitter: false`.
///
/// # Parameters
/// * `function` - The async function to retry
/// * `min_delay_millis` - Initial delay before first retry attempt (in milliseconds)
/// * `factor` - Exponential backoff multiplier for retry delays
/// * `max_times` - Maximum number of retry attempts
/// * `max_delay_seconds` - Maximum delay between retry attempts (in seconds)
pub async fn retry_function<FutureFn, Fut, T, E>(
function: FutureFn,
min_delay_millis: u64,
factor: f32,
max_times: usize,
max_delay_seconds: u64,
) -> Result<T, RetryError<E>>
where
Fut: Future<Output = Result<T, RetryError<E>>>,
FutureFn: FnMut() -> Fut,
{
let backoff = ExponentialBuilder::default()
.with_min_delay(Duration::from_millis(min_delay_millis))
.with_max_times(max_times)
.with_factor(factor)
.with_max_delay(Duration::from_secs(max_delay_seconds));
function
.retry(backoff)
.sleep(tokio::time::sleep)
.when(|e| matches!(e, RetryError::Transient(_)))
.await
}