qiniu_http_client/client/backoff/
mod.rs1mod exponential;
2mod fixed;
3mod limited;
4mod randomized;
5
6use super::{ResponseError, RetriedStatsInfo, RetryDecision};
7use auto_impl::auto_impl;
8use dyn_clonable::clonable;
9use qiniu_http::RequestParts as HttpRequestParts;
10use std::{
11 fmt::Debug,
12 ops::{Deref, DerefMut},
13 time::Duration,
14};
15
16#[clonable]
18#[auto_impl(&, &mut, Box, Rc, Arc)]
19pub trait Backoff: Clone + Debug + Sync + Send {
20 fn time(&self, request: &mut HttpRequestParts, opts: BackoffOptions) -> GotBackoffDuration;
22}
23
24#[derive(Copy, Debug, Clone)]
26pub struct BackoffOptions<'a> {
27 retry_decision: RetryDecision,
28 response_error: &'a ResponseError,
29 retried: &'a RetriedStatsInfo,
30}
31
32impl<'a> BackoffOptions<'a> {
33 #[inline]
35 pub fn builder(response_error: &'a ResponseError, retried: &'a RetriedStatsInfo) -> BackoffOptionsBuilder<'a> {
36 BackoffOptionsBuilder::new(response_error, retried)
37 }
38
39 #[inline]
41 pub fn retry_decision(&self) -> RetryDecision {
42 self.retry_decision
43 }
44
45 #[inline]
47 pub fn response_error(&self) -> &ResponseError {
48 self.response_error
49 }
50
51 #[inline]
53 pub fn retried(&self) -> &RetriedStatsInfo {
54 self.retried
55 }
56}
57
58#[derive(Copy, Debug, Clone)]
60pub struct BackoffOptionsBuilder<'a>(BackoffOptions<'a>);
61
62impl<'a> BackoffOptionsBuilder<'a> {
63 #[inline]
65 pub fn new(response_error: &'a ResponseError, retried: &'a RetriedStatsInfo) -> Self {
66 Self(BackoffOptions {
67 response_error,
68 retried,
69 retry_decision: Default::default(),
70 })
71 }
72
73 #[inline]
75 pub fn retry_decision(&mut self, decision: RetryDecision) -> &mut Self {
76 self.0.retry_decision = decision;
77 self
78 }
79
80 pub fn build(&self) -> BackoffOptions<'a> {
82 self.0
83 }
84}
85
86#[derive(Debug)]
88pub struct GotBackoffDuration(Duration);
89
90impl GotBackoffDuration {
91 #[inline]
93 pub fn duration(&self) -> Duration {
94 self.0
95 }
96
97 #[inline]
99 pub fn duration_mut(&mut self) -> &mut Duration {
100 &mut self.0
101 }
102}
103
104impl From<Duration> for GotBackoffDuration {
105 #[inline]
106 fn from(duration: Duration) -> Self {
107 Self(duration)
108 }
109}
110
111impl From<GotBackoffDuration> for Duration {
112 #[inline]
113 fn from(backoff_duration: GotBackoffDuration) -> Self {
114 backoff_duration.0
115 }
116}
117
118impl AsRef<Duration> for GotBackoffDuration {
119 #[inline]
120 fn as_ref(&self) -> &Duration {
121 &self.0
122 }
123}
124
125impl AsMut<Duration> for GotBackoffDuration {
126 #[inline]
127 fn as_mut(&mut self) -> &mut Duration {
128 &mut self.0
129 }
130}
131
132impl Deref for GotBackoffDuration {
133 type Target = Duration;
134
135 #[inline]
136 fn deref(&self) -> &Self::Target {
137 &self.0
138 }
139}
140
141impl DerefMut for GotBackoffDuration {
142 #[inline]
143 fn deref_mut(&mut self) -> &mut Self::Target {
144 &mut self.0
145 }
146}
147
148pub use exponential::ExponentialBackoff;
149pub use fixed::{FixedBackoff, NO_BACKOFF};
150pub use limited::LimitedBackoff;
151pub use randomized::{RandomizedBackoff, Ratio};