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
63
64
65
66
use super::{Backoff, BackoffOptions, GotBackoffDuration};
use qiniu_http::RequestParts as HttpRequestParts;
use std::time::Duration;
#[derive(Debug, Clone)]
pub struct LimitedBackoff<P: ?Sized> {
max_backoff: Duration,
min_backoff: Duration,
base_backoff: P,
}
impl<P> LimitedBackoff<P> {
#[inline]
pub const fn new(base_backoff: P, min_backoff: Duration, max_backoff: Duration) -> Self {
Self {
base_backoff,
min_backoff,
max_backoff,
}
}
#[inline]
pub const fn base_backoff(&self) -> &P {
&self.base_backoff
}
#[inline]
pub const fn max_backoff(&self) -> Duration {
self.max_backoff
}
#[inline]
pub const fn min_backoff(&self) -> Duration {
self.min_backoff
}
}
impl<P: Backoff> Backoff for LimitedBackoff<P> {
#[inline]
fn time(&self, request: &mut HttpRequestParts, opts: BackoffOptions) -> GotBackoffDuration {
self.base_backoff
.time(request, opts)
.duration()
.max(self.min_backoff)
.min(self.max_backoff)
.into()
}
}
impl<P: Default> Default for LimitedBackoff<P> {
#[inline]
fn default() -> Self {
LimitedBackoff::new(P::default(), Duration::from_secs(0), Duration::from_secs(300))
}
}