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
use super::{Backoff, BackoffOptions, GotBackoffDuration, RetryDecision};
use qiniu_http::RequestParts as HttpRequestParts;
use std::time::Duration;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ExponentialBackoff {
base_number: u32,
base_delay: Duration,
}
impl ExponentialBackoff {
#[inline]
pub const fn new(base_number: u32, base_delay: Duration) -> Self {
Self {
base_number,
base_delay,
}
}
#[inline]
pub const fn base_number(&self) -> u32 {
self.base_number
}
#[inline]
pub const fn base_delay(&self) -> Duration {
self.base_delay
}
}
impl Backoff for ExponentialBackoff {
fn time(&self, _request: &mut HttpRequestParts, opts: BackoffOptions) -> GotBackoffDuration {
let retried_count = if opts.retry_decision() == RetryDecision::Throttled {
opts.retried().retried_total()
} else {
opts.retried().retried_on_current_endpoint()
};
GotBackoffDuration::from(self.base_delay * self.base_number.pow(retried_count as u32))
}
}
impl Default for ExponentialBackoff {
#[inline]
fn default() -> Self {
ExponentialBackoff::new(2, Duration::from_millis(100))
}
}