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
use super::{Backoff, BackoffOptions, GotBackoffDuration};
use qiniu_http::RequestParts as HttpRequestParts;
use std::time::Duration;

/// 无退避的退避时长提供者
pub const NO_BACKOFF: FixedBackoff = FixedBackoff::new(Duration::from_nanos(0));

/// 固定时长的退避时长提供者
///
/// 默认时长为 100 毫秒
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FixedBackoff {
    delay: Duration,
}

impl FixedBackoff {
    /// 创建固定时长的退避时长提供者
    #[inline]
    pub const fn new(delay: Duration) -> Self {
        Self { delay }
    }

    /// 获取固定时长
    #[inline]
    pub const fn delay(&self) -> Duration {
        self.delay
    }
}

impl Backoff for FixedBackoff {
    #[inline]
    fn time(&self, _request: &mut HttpRequestParts, _opts: BackoffOptions) -> GotBackoffDuration {
        self.delay.into()
    }
}

impl Default for FixedBackoff {
    #[inline]
    fn default() -> Self {
        FixedBackoff::new(Duration::from_millis(100))
    }
}