use super::{Backoff, BackoffOptions, GotBackoffDuration, RetryDecision};
use qiniu_http::RequestParts as HttpRequestParts;
use std::time::Duration;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ExponentialBackoff {
base_number: u32,
base_delay: Duration,
}
impl ExponentialBackoff {
#[inline]
pub const fn new(base_number: u32, base_delay: Duration) -> Self {
Self {
base_number,
base_delay,
}
}
#[inline]
pub const fn base_number(&self) -> u32 {
self.base_number
}
#[inline]
pub const fn base_delay(&self) -> Duration {
self.base_delay
}
}
impl Backoff for ExponentialBackoff {
fn time(&self, _request: &mut HttpRequestParts, opts: BackoffOptions) -> GotBackoffDuration {
let retried_count = if opts.retry_decision() == RetryDecision::Throttled {
opts.retried().retried_total()
} else {
opts.retried().retried_on_current_endpoint()
};
GotBackoffDuration::from(self.base_delay * self.base_number.pow(retried_count as u32))
}
}
impl Default for ExponentialBackoff {
#[inline]
fn default() -> Self {
ExponentialBackoff::new(2, Duration::from_millis(100))
}
}