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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
mod error;
mod limited;
mod never;

use super::{Idempotent, ResponseError, RetriedStatsInfo};
use auto_impl::auto_impl;
use qiniu_http::RequestParts as HttpRequestParts;
use smart_default::SmartDefault;
use std::{
    fmt::{self, Debug},
    ops::{Deref, DerefMut},
};

/// 请求重试器
///
/// 根据 HTTP 客户端返回的错误,决定是否重试请求,重试决定由 [`RetryDecision`] 定义。
#[auto_impl(&, &mut, Box, Rc, Arc)]
pub trait RequestRetrier: Debug + Sync + Send {
    /// 作出重试决定
    fn retry(&self, request: &mut HttpRequestParts, opts: RequestRetrierOptions<'_>) -> RetryResult;
}

/// 重试决定
#[derive(Copy, Clone, Debug, Eq, PartialEq, SmartDefault)]
#[non_exhaustive]
pub enum RetryDecision {
    /// 不再重试
    #[default]
    DontRetry,

    /// 切换到下一个服务器
    TryNextServer,

    /// 切换到备选终端地址
    TryAlternativeEndpoints,

    /// 重试当前请求
    RetryRequest,

    /// 节流
    Throttled,
}

/// 重试器选项
#[derive(Copy, Debug, Clone)]
pub struct RequestRetrierOptions<'a> {
    idempotent: Idempotent,
    response_error: &'a ResponseError,
    retried: &'a RetriedStatsInfo,
}

impl<'a> RequestRetrierOptions<'a> {
    pub(super) fn new(
        idempotent: Idempotent,
        response_error: &'a ResponseError,
        retried: &'a RetriedStatsInfo,
    ) -> Self {
        Self {
            idempotent,
            response_error,
            retried,
        }
    }

    /// 是否是幂等请求
    #[inline]
    pub fn idempotent(&self) -> Idempotent {
        self.idempotent
    }

    /// 获取响应错误
    #[inline]
    pub fn response_error(&self) -> &ResponseError {
        self.response_error
    }

    /// 获取重试统计信息
    #[inline]
    pub fn retried(&self) -> &RetriedStatsInfo {
        self.retried
    }
}

/// 重试器结果
#[derive(Clone)]
pub struct RetryResult(RetryDecision);

impl RetryResult {
    /// 获取重试决定
    #[inline]
    pub fn decision(&self) -> RetryDecision {
        self.0
    }

    /// 获取重试决定的可变引用
    #[inline]
    pub fn decision_mut(&mut self) -> &mut RetryDecision {
        &mut self.0
    }
}

impl From<RetryDecision> for RetryResult {
    #[inline]
    fn from(decision: RetryDecision) -> Self {
        Self(decision)
    }
}

impl From<RetryResult> for RetryDecision {
    #[inline]
    fn from(result: RetryResult) -> Self {
        result.0
    }
}

impl AsRef<RetryDecision> for RetryResult {
    #[inline]
    fn as_ref(&self) -> &RetryDecision {
        &self.0
    }
}

impl AsMut<RetryDecision> for RetryResult {
    #[inline]
    fn as_mut(&mut self) -> &mut RetryDecision {
        &mut self.0
    }
}

impl Deref for RetryResult {
    type Target = RetryDecision;

    #[inline]
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl DerefMut for RetryResult {
    #[inline]
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl Debug for RetryResult {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.0.fmt(f)
    }
}

pub use error::ErrorRetrier;
pub use limited::LimitedRetrier;
pub use never::NeverRetrier;