azure_core/policies/retry_policies/
fixed_retry.rs

1use std::time::Duration;
2
3/// Retry policy with a fixed back-off.
4///
5/// Retry policy with fixed back-off (with an added random delay up to 256 ms). Each retry will
6/// happen at least after the same, configured sleep time. The policy will retry until the maximum number of
7/// retries have been reached or the maximum allowed delay has passed (whichever comes first). The
8/// wait time is not precise.
9#[derive(Debug, Clone, PartialEq, Eq)]
10pub struct FixedRetryPolicy {
11    delay: Duration,
12    max_retries: u32,
13    max_elapsed: Duration,
14}
15
16impl FixedRetryPolicy {
17    pub(crate) fn new(delay: Duration, max_retries: u32, max_elapsed: Duration) -> Self {
18        Self {
19            delay: delay.max(Duration::from_millis(10)),
20            max_retries,
21            max_elapsed,
22        }
23    }
24}
25
26impl super::RetryPolicy for FixedRetryPolicy {
27    fn is_expired(&self, time_since_start: Duration, retry_count: u32) -> bool {
28        retry_count >= self.max_retries || time_since_start >= self.max_elapsed
29    }
30
31    fn sleep_duration(&self, _retry_count: u32) -> Duration {
32        let sleep_ms = self.delay.as_millis() as u64 + u64::from(rand::random::<u8>());
33        Duration::from_millis(sleep_ms)
34    }
35}