Struct qiniu_http_client::GotBackoffDuration
source · pub struct GotBackoffDuration(_);
Expand description
获取的退避时长
Implementations§
source§impl GotBackoffDuration
impl GotBackoffDuration
sourcepub fn duration(&self) -> Duration
pub fn duration(&self) -> Duration
获取退避时长
Examples found in repository?
More examples
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
fn backoff(
http_request: &mut SyncHttpRequest<'_>,
parts: &InnerRequestParts<'_>,
retried: &mut RetriedStatsInfo,
err: &TryError,
) -> Result<(), TryError> {
let delay = parts
.http_client()
.backoff()
.time(
http_request,
BackoffOptions::builder(err.response_error(), retried)
.retry_decision(err.retry_decision())
.build(),
)
.duration();
call_before_backoff_callbacks(parts, http_request, retried, delay)?;
if delay > Duration::new(0, 0) {
sleep(delay);
}
call_after_backoff_callbacks(parts, http_request, retried, delay)?;
Ok(())
}
}
fn need_backoff(err: &TryError) -> bool {
matches!(
err.retry_decision(),
RetryDecision::RetryRequest | RetryDecision::Throttled | RetryDecision::TryNextServer
)
}
fn need_retry_after_backoff(err: &TryError) -> bool {
matches!(
err.retry_decision(),
RetryDecision::RetryRequest | RetryDecision::Throttled
)
}
fn handle_response_error(
mut response_error: ResponseError,
http_parts: &mut HttpRequestParts,
parts: &InnerRequestParts<'_>,
retried: &mut RetriedStatsInfo,
) -> TryError {
let retry_result = parts.http_client().request_retrier().retry(
http_parts,
RequestRetrierOptions::builder(&response_error, retried)
.idempotent(parts.idempotent())
.build(),
);
retried.increase_current_endpoint();
response_error = response_error.set_retry_decision(retry_result.decision());
TryError::new(response_error, retry_result)
}
#[cfg(feature = "async")]
mod async_send {
use super::{
super::{super::AsyncResponse, utils::async_judge},
*,
};
use futures_timer::Delay as AsyncDelay;
use qiniu_http::AsyncRequest as AsyncHttpRequest;
pub(in super::super) async fn async_send_http_request(
http_request: &mut AsyncHttpRequest<'_>,
parts: &InnerRequestParts<'_>,
retried: &mut RetriedStatsInfo,
) -> Result<AsyncResponse, TryError> {
loop {
let mut response = parts
.http_client()
.http_caller()
.async_call(http_request)
.await
.map_err(ResponseError::from)
.and_then(|response| {
call_response_callbacks(parts, http_request, retried, response.parts()).map(|_| response)
})
.map(AsyncResponse::from);
if let Ok(resp) = response {
response = async_judge(resp, retried).await
};
let response = response.map_err(|err| handle_response_error(err, http_request, parts, retried));
match response {
Ok(response) => {
return Ok(response);
}
Err(mut err) => {
call_error_callbacks(parts, http_request, retried, err.response_error_mut())?;
if need_backoff(&err) {
backoff(http_request, parts, retried, &err).await?;
if need_retry_after_backoff(&err) {
continue;
}
}
return Err(err);
}
}
}
async fn backoff(
http_request: &mut AsyncHttpRequest<'_>,
parts: &InnerRequestParts<'_>,
retried: &mut RetriedStatsInfo,
err: &TryError,
) -> Result<(), TryError> {
let delay = parts
.http_client()
.backoff()
.time(
http_request,
BackoffOptions::builder(err.response_error(), retried)
.retry_decision(err.retry_decision())
.build(),
)
.duration();
call_before_backoff_callbacks(parts, http_request, retried, delay)?;
if delay > Duration::new(0, 0) {
async_sleep(delay).await;
}
call_after_backoff_callbacks(parts, http_request, retried, delay)?;
Ok(())
}
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
fn time(&self, request: &mut HttpRequestParts, opts: BackoffOptions) -> GotBackoffDuration {
let duration = self.base_backoff().time(request, opts).duration();
let minification: Ratio<u128> = Ratio::new_raw(
self.minification().numer().to_owned().into(),
self.minification().denom().to_owned().into(),
);
let magnification: Ratio<u128> = Ratio::new_raw(
self.magnification().numer().to_owned().into(),
self.magnification().denom().to_owned().into(),
);
let minified: u64 = (minification * duration.as_nanos())
.to_integer()
.try_into()
.unwrap_or(u64::MAX);
let magnified: u64 = (magnification * duration.as_nanos())
.to_integer()
.try_into()
.unwrap_or(u64::MAX);
let randomized = thread_rng().gen_range(minified, magnified);
Duration::from_nanos(randomized).into()
}
sourcepub fn duration_mut(&mut self) -> &mut Duration
pub fn duration_mut(&mut self) -> &mut Duration
获取退避时长的可变引用
Methods from Deref<Target = Duration>§
pub const SECOND: Duration = Duration::from_secs(1)
pub const MILLISECOND: Duration = Duration::from_millis(1)
pub const MICROSECOND: Duration = Duration::from_micros(1)
pub const NANOSECOND: Duration = Duration::from_nanos(1)
pub const ZERO: Duration = Duration::from_nanos(0)
pub const MAX: Duration = Duration::new(u64::MAX, NANOS_PER_SEC - 1)
1.53.0 · sourcepub fn is_zero(&self) -> bool
pub fn is_zero(&self) -> bool
Returns true if this Duration
spans no time.
Examples
use std::time::Duration;
assert!(Duration::ZERO.is_zero());
assert!(Duration::new(0, 0).is_zero());
assert!(Duration::from_nanos(0).is_zero());
assert!(Duration::from_secs(0).is_zero());
assert!(!Duration::new(1, 1).is_zero());
assert!(!Duration::from_nanos(1).is_zero());
assert!(!Duration::from_secs(1).is_zero());
1.3.0 · sourcepub fn as_secs(&self) -> u64
pub fn as_secs(&self) -> u64
Returns the number of whole seconds contained by this Duration
.
The returned value does not include the fractional (nanosecond) part of the
duration, which can be obtained using subsec_nanos
.
Examples
use std::time::Duration;
let duration = Duration::new(5, 730023852);
assert_eq!(duration.as_secs(), 5);
To determine the total number of seconds represented by the Duration
including the fractional part, use as_secs_f64
or as_secs_f32
1.27.0 · sourcepub fn subsec_millis(&self) -> u32
pub fn subsec_millis(&self) -> u32
Returns the fractional part of this Duration
, in whole milliseconds.
This method does not return the length of the duration when represented by milliseconds. The returned number always represents a fractional portion of a second (i.e., it is less than one thousand).
Examples
use std::time::Duration;
let duration = Duration::from_millis(5432);
assert_eq!(duration.as_secs(), 5);
assert_eq!(duration.subsec_millis(), 432);
1.27.0 · sourcepub fn subsec_micros(&self) -> u32
pub fn subsec_micros(&self) -> u32
Returns the fractional part of this Duration
, in whole microseconds.
This method does not return the length of the duration when represented by microseconds. The returned number always represents a fractional portion of a second (i.e., it is less than one million).
Examples
use std::time::Duration;
let duration = Duration::from_micros(1_234_567);
assert_eq!(duration.as_secs(), 1);
assert_eq!(duration.subsec_micros(), 234_567);
1.3.0 · sourcepub fn subsec_nanos(&self) -> u32
pub fn subsec_nanos(&self) -> u32
Returns the fractional part of this Duration
, in nanoseconds.
This method does not return the length of the duration when represented by nanoseconds. The returned number always represents a fractional portion of a second (i.e., it is less than one billion).
Examples
use std::time::Duration;
let duration = Duration::from_millis(5010);
assert_eq!(duration.as_secs(), 5);
assert_eq!(duration.subsec_nanos(), 10_000_000);
1.33.0 · sourcepub fn as_millis(&self) -> u128
pub fn as_millis(&self) -> u128
Returns the total number of whole milliseconds contained by this Duration
.
Examples
use std::time::Duration;
let duration = Duration::new(5, 730023852);
assert_eq!(duration.as_millis(), 5730);
1.33.0 · sourcepub fn as_micros(&self) -> u128
pub fn as_micros(&self) -> u128
Returns the total number of whole microseconds contained by this Duration
.
Examples
use std::time::Duration;
let duration = Duration::new(5, 730023852);
assert_eq!(duration.as_micros(), 5730023);
1.33.0 · sourcepub fn as_nanos(&self) -> u128
pub fn as_nanos(&self) -> u128
Returns the total number of nanoseconds contained by this Duration
.
Examples
use std::time::Duration;
let duration = Duration::new(5, 730023852);
assert_eq!(duration.as_nanos(), 5730023852);
1.38.0 · sourcepub fn as_secs_f64(&self) -> f64
pub fn as_secs_f64(&self) -> f64
Returns the number of seconds contained by this Duration
as f64
.
The returned value does include the fractional (nanosecond) part of the duration.
Examples
use std::time::Duration;
let dur = Duration::new(2, 700_000_000);
assert_eq!(dur.as_secs_f64(), 2.7);
1.38.0 · sourcepub fn as_secs_f32(&self) -> f32
pub fn as_secs_f32(&self) -> f32
Returns the number of seconds contained by this Duration
as f32
.
The returned value does include the fractional (nanosecond) part of the duration.
Examples
use std::time::Duration;
let dur = Duration::new(2, 700_000_000);
assert_eq!(dur.as_secs_f32(), 2.7);
Trait Implementations§
source§impl AsMut<Duration> for GotBackoffDuration
impl AsMut<Duration> for GotBackoffDuration
source§impl AsRef<Duration> for GotBackoffDuration
impl AsRef<Duration> for GotBackoffDuration
source§impl Debug for GotBackoffDuration
impl Debug for GotBackoffDuration
source§impl Deref for GotBackoffDuration
impl Deref for GotBackoffDuration
source§impl DerefMut for GotBackoffDuration
impl DerefMut for GotBackoffDuration
source§impl From<Duration> for GotBackoffDuration
impl From<Duration> for GotBackoffDuration
source§impl From<GotBackoffDuration> for Duration
impl From<GotBackoffDuration> for Duration
source§fn from(backoff_duration: GotBackoffDuration) -> Self
fn from(backoff_duration: GotBackoffDuration) -> Self
Auto Trait Implementations§
impl RefUnwindSafe for GotBackoffDuration
impl Send for GotBackoffDuration
impl Sync for GotBackoffDuration
impl Unpin for GotBackoffDuration
impl UnwindSafe for GotBackoffDuration
Blanket Implementations§
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read moresource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read moresource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> Rwhere
Self: Borrow<B>,
B: 'a + ?Sized,
R: 'a,
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> Rwhere
Self: Borrow<B>,
B: 'a + ?Sized,
R: 'a,
source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R
) -> Rwhere
Self: BorrowMut<B>,
B: 'a + ?Sized,
R: 'a,
fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R
) -> Rwhere
Self: BorrowMut<B>,
B: 'a + ?Sized,
R: 'a,
source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> Rwhere
Self: AsRef<U>,
U: 'a + ?Sized,
R: 'a,
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> Rwhere
Self: AsRef<U>,
U: 'a + ?Sized,
R: 'a,
self
, then passes self.as_ref()
into the pipe function.source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> Rwhere
Self: AsMut<U>,
U: 'a + ?Sized,
R: 'a,
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> Rwhere
Self: AsMut<U>,
U: 'a + ?Sized,
R: 'a,
self
, then passes self.as_mut()
into the pipe
function.source§impl<T> Tap for T
impl<T> Tap for T
source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Selfwhere
Self: Borrow<B>,
B: ?Sized,
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Selfwhere
Self: Borrow<B>,
B: ?Sized,
Borrow<B>
of a value. Read moresource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere
Self: BorrowMut<B>,
B: ?Sized,
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere
Self: BorrowMut<B>,
B: ?Sized,
BorrowMut<B>
of a value. Read moresource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Selfwhere
Self: AsRef<R>,
R: ?Sized,
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Selfwhere
Self: AsRef<R>,
R: ?Sized,
AsRef<R>
view of a value. Read moresource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere
Self: AsMut<R>,
R: ?Sized,
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere
Self: AsMut<R>,
R: ?Sized,
AsMut<R>
view of a value. Read moresource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Selfwhere
Self: Deref<Target = T>,
T: ?Sized,
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Selfwhere
Self: Deref<Target = T>,
T: ?Sized,
Deref::Target
of a value. Read moresource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Selfwhere
Self: DerefMut<Target = T> + Deref,
T: ?Sized,
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Selfwhere
Self: DerefMut<Target = T> + Deref,
T: ?Sized,
Deref::Target
of a value. Read moresource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap()
only in debug builds, and is erased in release builds.source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut()
only in debug builds, and is erased in release
builds.source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Selfwhere
Self: Borrow<B>,
B: ?Sized,
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Selfwhere
Self: Borrow<B>,
B: ?Sized,
.tap_borrow()
only in debug builds, and is erased in release
builds.source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere
Self: BorrowMut<B>,
B: ?Sized,
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere
Self: BorrowMut<B>,
B: ?Sized,
.tap_borrow_mut()
only in debug builds, and is erased in release
builds.source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Selfwhere
Self: AsRef<R>,
R: ?Sized,
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Selfwhere
Self: AsRef<R>,
R: ?Sized,
.tap_ref()
only in debug builds, and is erased in release
builds.source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere
Self: AsMut<R>,
R: ?Sized,
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere
Self: AsMut<R>,
R: ?Sized,
.tap_ref_mut()
only in debug builds, and is erased in release
builds.