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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
mod error;
mod limited;
mod never;
use super::{Idempotent, ResponseError, RetriedStatsInfo};
use auto_impl::auto_impl;
use dyn_clonable::clonable;
use qiniu_http::RequestParts as HttpRequestParts;
use smart_default::SmartDefault;
use std::{
fmt::{self, Debug},
ops::{Deref, DerefMut},
};
#[clonable]
#[auto_impl(&, &mut, Box, Rc, Arc)]
pub trait RequestRetrier: Clone + 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> {
#[inline]
pub fn builder(
response_error: &'a ResponseError,
retried: &'a RetriedStatsInfo,
) -> RequestRetrierOptionsBuilder<'a> {
RequestRetrierOptionsBuilder::new(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(Copy, Debug, Clone)]
pub struct RequestRetrierOptionsBuilder<'a>(RequestRetrierOptions<'a>);
impl<'a> RequestRetrierOptionsBuilder<'a> {
#[inline]
pub fn new(response_error: &'a ResponseError, retried: &'a RetriedStatsInfo) -> Self {
Self(RequestRetrierOptions {
response_error,
retried,
idempotent: Default::default(),
})
}
#[inline]
pub fn idempotent(&mut self, idempotent: Idempotent) -> &mut Self {
self.0.idempotent = idempotent;
self
}
#[inline]
pub fn build(&self) -> RequestRetrierOptions<'a> {
self.0
}
}
#[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;