use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::OnceLock;
use std::time::Duration;
use http::Method;
use crate::body::RequestBody;
use crate::error::Error;
const DEFAULT_RETRYABLE_STATUSES: &[u16] = &[408, 429, 502, 503, 504];
#[cfg(any(feature = "transport-http1", feature = "transport-http2"))]
fn is_hyper_canceled(inner: &hyper_util::client::legacy::Error) -> bool {
crate::error::hyper_legacy_is_canceled(inner)
}
const DEFAULT_RETRYABLE_METHODS: &[Method] = &[Method::GET, Method::HEAD, Method::OPTIONS];
#[derive(Debug, Clone)]
pub struct RetryPolicy {
max_attempts: usize,
max_elapsed: Option<Duration>,
backoff: BackoffPolicy,
method_policy: MethodPolicy,
status_policy: StatusPolicy,
respect_retry_after: bool,
}
impl Default for RetryPolicy {
fn default() -> Self {
Self {
max_attempts: 1,
max_elapsed: None,
backoff: BackoffPolicy::default(),
method_policy: MethodPolicy::default(),
status_policy: StatusPolicy::default(),
respect_retry_after: false,
}
}
}
impl RetryPolicy {
#[must_use]
pub fn builder() -> RetryPolicyBuilder {
RetryPolicyBuilder::new()
}
#[must_use]
pub fn max_attempts(&self) -> usize {
self.max_attempts
}
#[must_use]
pub fn max_elapsed(&self) -> Option<Duration> {
self.max_elapsed
}
#[must_use]
pub fn backoff(&self) -> &BackoffPolicy {
&self.backoff
}
#[must_use]
pub fn method_policy(&self) -> &MethodPolicy {
&self.method_policy
}
#[must_use]
pub fn status_policy(&self) -> &StatusPolicy {
&self.status_policy
}
#[must_use]
pub fn respect_retry_after(&self) -> bool {
self.respect_retry_after
}
#[must_use]
pub fn is_enabled(&self) -> bool {
self.max_attempts > 1
}
#[must_use]
pub fn is_method_retryable(&self, method: &Method) -> bool {
self.method_policy.is_retryable(method)
}
#[must_use]
pub fn is_status_retryable(&self, status: u16) -> bool {
self.status_policy.is_retryable(status)
}
#[must_use]
pub fn is_error_retryable(error: &Error) -> bool {
#[cfg(any(feature = "transport-http1", feature = "transport-http2"))]
if let Error::HyperClient(inner) = error {
return inner.is_connect() || is_hyper_canceled(inner.as_ref());
}
match error {
Error::Connect(_)
| Error::Io(_)
| Error::Hyper(_)
| Error::Timeout {
phase: crate::timeout::TimeoutPhase::Connect,
..
}
| Error::Timeout {
phase: crate::timeout::TimeoutPhase::Pool,
..
}
| Error::Timeout {
phase:
crate::timeout::TimeoutPhase::ProxyConnect | crate::timeout::TimeoutPhase::ProxyTls,
..
}
| Error::H3Connect(_) => true,
Error::CustomTransport(error) => matches!(
error.kind(),
crate::transport::dialer::DialErrorKind::Connection
| crate::transport::dialer::DialErrorKind::Timeout
),
Error::Http2StreamReset { reason } if reason.starts_with("REFUSED_STREAM") => true,
_ => false,
}
}
#[must_use]
pub fn backoff_delay(&self, attempt: usize) -> Option<Duration> {
self.backoff.delay(attempt)
}
#[must_use]
pub fn retry_after_delay(&self, retry_after: &str) -> Option<Duration> {
if !self.respect_retry_after {
return None;
}
let delay = if let Ok(secs) = retry_after.trim().parse::<u64>() {
Duration::from_secs(secs)
} else if let Ok(date) = httpdate::parse_http_date(retry_after.trim()) {
match date.duration_since(std::time::SystemTime::now()) {
Ok(duration) => duration,
Err(_) => return None,
}
} else {
return None;
};
let cap = self.backoff.max_delay;
Some(if delay > cap { cap } else { delay })
}
#[must_use]
pub fn check_body_replayable(body: &RequestBody) -> ReplayCheck {
if body.is_replayable() {
ReplayCheck::Replayable
} else {
ReplayCheck::NotReplayable
}
}
}
pub struct RetryPolicyBuilder {
max_attempts: usize,
max_elapsed: Option<Duration>,
backoff: BackoffPolicyBuilder,
method_policy: MethodPolicy,
status_policy: StatusPolicy,
respect_retry_after: bool,
custom_statuses_used: bool,
}
impl RetryPolicyBuilder {
#[must_use]
pub fn new() -> Self {
Self {
max_attempts: 1,
max_elapsed: None,
backoff: BackoffPolicyBuilder::new(),
method_policy: MethodPolicy::default(),
status_policy: StatusPolicy::default(),
respect_retry_after: false,
custom_statuses_used: false,
}
}
#[must_use]
pub fn max_attempts(mut self, max: usize) -> Self {
self.max_attempts = max;
self
}
#[must_use]
pub fn max_elapsed(mut self, elapsed: Duration) -> Self {
self.max_elapsed = Some(elapsed);
self
}
#[must_use]
pub fn backoff_factor(mut self, factor: f64) -> Self {
self.backoff = self.backoff.factor(factor);
self
}
#[must_use]
pub fn max_delay(mut self, delay: Duration) -> Self {
self.backoff = self.backoff.max_delay(delay);
self
}
#[must_use]
pub fn initial_delay(mut self, delay: Duration) -> Self {
self.backoff = self.backoff.initial_delay(delay);
self
}
#[must_use]
pub fn retry_status(mut self, status: u16) -> Self {
if !self.custom_statuses_used {
self.status_policy = StatusPolicy::default_empty();
self.custom_statuses_used = true;
}
self.status_policy.add_status(status);
self
}
#[must_use]
pub fn retry_statuses(mut self, statuses: impl IntoIterator<Item = u16>) -> Self {
self.status_policy = StatusPolicy::new(statuses);
self.custom_statuses_used = true;
self
}
#[must_use]
pub fn retry_method(mut self, method: Method) -> Self {
self.method_policy.add_method(method);
self
}
#[must_use]
pub fn allow_post_retry(mut self) -> Self {
self.method_policy.add_method(Method::POST);
self
}
#[must_use]
pub fn allow_put_retry(mut self) -> Self {
self.method_policy.add_method(Method::PUT);
self
}
#[must_use]
pub fn allow_delete_retry(mut self) -> Self {
self.method_policy.add_method(Method::DELETE);
self
}
#[must_use]
pub fn allow_patch_retry(mut self) -> Self {
self.method_policy.add_method(Method::PATCH);
self
}
#[must_use]
pub fn respect_retry_after(mut self, respect: bool) -> Self {
self.respect_retry_after = respect;
self
}
#[must_use]
pub fn build(self) -> RetryPolicy {
RetryPolicy {
max_attempts: self.max_attempts,
max_elapsed: self.max_elapsed,
backoff: self.backoff.build(),
method_policy: self.method_policy,
status_policy: self.status_policy,
respect_retry_after: self.respect_retry_after,
}
}
}
impl Default for RetryPolicyBuilder {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone)]
pub struct BackoffPolicy {
factor: f64,
max_delay: Duration,
initial_delay: Duration,
}
impl Default for BackoffPolicy {
fn default() -> Self {
Self {
factor: 2.0,
max_delay: Duration::from_secs(30),
initial_delay: Duration::from_millis(500),
}
}
}
impl BackoffPolicy {
#[must_use]
#[allow(
clippy::cast_precision_loss,
reason = "the attempt counter is only used as an f64 exponent, so rounding beyond 2^53 attempts cannot change the delay"
)]
pub fn delay(&self, attempt: usize) -> Option<Duration> {
if attempt <= 1 {
return None;
}
let exp = (attempt - 2) as f64;
let raw = self.initial_delay.as_secs_f64() * self.factor.powf(exp);
let max_float = self.max_delay.as_secs_f64();
let max_secs = if Duration::try_from_secs_f64(max_float).is_ok() {
max_float
} else {
f64::MAX
};
let capped = if !raw.is_finite() || raw < 0.0 || raw > max_secs {
self.max_delay
} else {
Duration::try_from_secs_f64(raw).unwrap_or(self.max_delay)
};
let jittered_secs = match get_random_f64() {
Some(jitter) => capped.as_secs_f64() * jitter,
None => capped.as_secs_f64(),
};
let final_delay = if !jittered_secs.is_finite() || jittered_secs < 0.0 {
capped
} else {
Duration::try_from_secs_f64(jittered_secs).unwrap_or(capped)
};
Some(final_delay.max(Duration::from_millis(1).min(capped)))
}
#[must_use]
pub fn max_delay(&self) -> Duration {
self.max_delay
}
#[must_use]
pub fn factor(&self) -> f64 {
self.factor
}
#[must_use]
pub fn initial_delay(&self) -> Duration {
self.initial_delay
}
}
pub struct BackoffPolicyBuilder {
factor: f64,
max_delay: Duration,
initial_delay: Duration,
}
impl BackoffPolicyBuilder {
#[must_use]
fn new() -> Self {
Self {
factor: 2.0,
max_delay: Duration::from_secs(30),
initial_delay: Duration::from_millis(500),
}
}
#[must_use]
fn factor(mut self, factor: f64) -> Self {
self.factor = factor;
self
}
#[must_use]
fn max_delay(mut self, delay: Duration) -> Self {
self.max_delay = delay;
self
}
#[must_use]
fn initial_delay(mut self, delay: Duration) -> Self {
self.initial_delay = delay;
self
}
#[must_use]
fn build(self) -> BackoffPolicy {
BackoffPolicy {
factor: self.factor,
max_delay: self.max_delay,
initial_delay: self.initial_delay,
}
}
}
#[derive(Debug, Clone)]
pub struct MethodPolicy {
methods: Vec<Method>,
}
impl Default for MethodPolicy {
fn default() -> Self {
Self {
methods: DEFAULT_RETRYABLE_METHODS.to_vec(),
}
}
}
impl MethodPolicy {
#[must_use]
pub fn new(methods: Vec<Method>) -> Self {
Self { methods }
}
pub fn add_method(&mut self, method: Method) {
if !self.methods.contains(&method) {
self.methods.push(method);
}
}
#[must_use]
pub fn is_retryable(&self, method: &Method) -> bool {
self.methods.contains(method)
}
#[must_use]
pub fn methods(&self) -> &[Method] {
&self.methods
}
}
#[derive(Debug, Clone)]
pub struct StatusPolicy {
statuses: Vec<u16>,
}
impl Default for StatusPolicy {
fn default() -> Self {
Self {
statuses: DEFAULT_RETRYABLE_STATUSES.to_vec(),
}
}
}
impl StatusPolicy {
#[must_use]
pub fn new(statuses: impl IntoIterator<Item = u16>) -> Self {
Self {
statuses: statuses.into_iter().collect(),
}
}
#[must_use]
pub fn default_empty() -> Self {
Self {
statuses: Vec::new(),
}
}
pub fn add_status(&mut self, status: u16) {
if !self.statuses.contains(&status) {
self.statuses.push(status);
}
}
#[must_use]
pub fn is_retryable(&self, status: u16) -> bool {
self.statuses.contains(&status)
}
#[must_use]
pub fn statuses(&self) -> &[u16] {
&self.statuses
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ReplayCheck {
Replayable,
NotReplayable,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RetryCause {
TransportError(String),
Status(u16),
Timeout(String),
}
impl std::fmt::Display for RetryCause {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::TransportError(msg) => write!(f, "transport error: {msg}"),
Self::Status(status) => write!(f, "retryable status: {status}"),
Self::Timeout(phase) => write!(f, "timeout: {phase}"),
}
}
}
#[derive(Debug, Clone)]
pub struct RetryContext {
pub attempt: usize,
pub method: Method,
pub body_replayable: bool,
pub cause: RetryCause,
pub remaining_total: Option<Duration>,
}
#[must_use]
pub fn should_retry(
policy: &RetryPolicy,
method: &Method,
body: &RequestBody,
error: Option<&Error>,
status: Option<u16>,
) -> Option<RetryCause> {
if !policy.is_method_retryable(method) {
return None;
}
if !body.is_replayable() {
return None;
}
if let Some(err) = error {
if RetryPolicy::is_error_retryable(err) {
return Some(RetryCause::TransportError(err.to_string()));
}
return None;
}
if let Some(status) = status {
if policy.is_status_retryable(status) {
return Some(RetryCause::Status(status));
}
}
None
}
#[allow(
clippy::cast_precision_loss,
reason = "the 53 high bits of the PRNG state are exactly representable in f64, so the conversion is lossless"
)]
fn get_random_f64() -> Option<f64> {
static STATE: OnceLock<AtomicU64> = OnceLock::new();
let state = if let Some(state) = STATE.get() {
state
} else {
let mut buf = [0u8; 8];
let seed = u64::from_le_bytes({
getrandom::getrandom(&mut buf).ok()?;
buf
})
.max(1);
let _ = STATE.set(AtomicU64::new(seed));
STATE.get()?
};
#[allow(
deprecated,
reason = "fetch_update is deprecated in favour of try_update, which is still unstable on the MSRV toolchain"
)]
let val = state
.fetch_update(Ordering::Relaxed, Ordering::Relaxed, |mut value| {
value ^= value << 13;
value ^= value >> 7;
value ^= value << 17;
Some(value.max(1))
})
.ok()?;
Some(((val >> 11) as f64) / ((1u64 << 53) as f64))
}
#[cfg(test)]
mod tests {
use super::*;
use proptest::prelude::*;
#[test]
fn retry_policy_default_disables_retries() {
let policy = RetryPolicy::default();
assert!(!policy.is_enabled());
assert_eq!(policy.max_attempts(), 1);
}
#[test]
fn proxy_timeout_phases_are_retryable() {
for phase in [
crate::timeout::TimeoutPhase::ProxyConnect,
crate::timeout::TimeoutPhase::ProxyTls,
] {
let error = Error::Timeout {
phase,
elapsed: Duration::from_secs(1),
};
assert!(RetryPolicy::is_error_retryable(&error));
}
}
#[test]
fn custom_dialer_connection_and_timeout_failures_are_retryable() {
for kind in [
crate::transport::dialer::DialErrorKind::Connection,
crate::transport::dialer::DialErrorKind::Timeout,
] {
let error = Error::CustomTransport(std::sync::Arc::new(
crate::transport::dialer::DialError::new(kind, "route failed"),
));
assert!(RetryPolicy::is_error_retryable(&error));
}
for kind in [
crate::transport::dialer::DialErrorKind::Authentication,
crate::transport::dialer::DialErrorKind::Rejected,
crate::transport::dialer::DialErrorKind::Other,
] {
let error = Error::CustomTransport(std::sync::Arc::new(
crate::transport::dialer::DialError::new(kind, "route failed"),
));
assert!(!RetryPolicy::is_error_retryable(&error));
}
}
#[test]
fn jitter_is_strictly_below_one() {
for _ in 0..10_000 {
let Some(jitter) = get_random_f64() else {
return;
};
assert!((0.0..1.0).contains(&jitter));
}
}
#[test]
fn retry_policy_builder_basic() {
let policy = RetryPolicy::builder()
.max_attempts(3)
.backoff_factor(0.2)
.build();
assert!(policy.is_enabled());
assert_eq!(policy.max_attempts(), 3);
}
#[test]
fn retry_policy_method_default_safe() {
let policy = RetryPolicy::default();
assert!(policy.is_method_retryable(&Method::GET));
assert!(policy.is_method_retryable(&Method::HEAD));
assert!(policy.is_method_retryable(&Method::OPTIONS));
assert!(!policy.is_method_retryable(&Method::POST));
assert!(!policy.is_method_retryable(&Method::PUT));
assert!(!policy.is_method_retryable(&Method::DELETE));
assert!(!policy.is_method_retryable(&Method::PATCH));
}
#[test]
fn retry_policy_method_allow_post() {
let policy = RetryPolicy::builder()
.max_attempts(3)
.allow_post_retry()
.build();
assert!(policy.is_method_retryable(&Method::POST));
}
#[test]
fn retry_policy_status_default() {
let policy = RetryPolicy::default();
assert!(policy.is_status_retryable(408));
assert!(policy.is_status_retryable(429));
assert!(policy.is_status_retryable(502));
assert!(policy.is_status_retryable(503));
assert!(policy.is_status_retryable(504));
assert!(!policy.is_status_retryable(200));
assert!(!policy.is_status_retryable(404));
}
#[test]
fn retry_policy_status_custom() {
let policy = RetryPolicy::builder()
.max_attempts(3)
.retry_status(429)
.retry_status(503)
.build();
assert!(policy.is_status_retryable(429));
assert!(policy.is_status_retryable(503));
assert!(!policy.is_status_retryable(408));
assert!(!policy.is_status_retryable(504));
}
#[test]
fn backoff_delay_none_for_first_attempt() {
let backoff = BackoffPolicy::default();
assert!(backoff.delay(1).is_none());
}
#[test]
fn backoff_delay_bounded_for_later_attempts() {
let backoff = BackoffPolicy {
factor: 0.5,
max_delay: Duration::from_secs(30),
initial_delay: Duration::from_millis(100),
};
let d2 = backoff.delay(2).unwrap();
assert!(d2 <= Duration::from_millis(100));
let d3 = backoff.delay(3).unwrap();
assert!(d3 <= Duration::from_millis(50));
}
#[test]
fn backoff_delay_has_minimum_when_cap_is_positive() {
let backoff = BackoffPolicy {
factor: 1.0,
max_delay: Duration::from_millis(1),
initial_delay: Duration::from_millis(1),
};
assert_eq!(backoff.delay(2), Some(Duration::from_millis(1)));
}
#[test]
fn default_backoff_grows_exponentially() {
let backoff = BackoffPolicy::default();
assert!((backoff.factor() - 2.0).abs() < f64::EPSILON);
assert!(backoff.delay(2).unwrap() <= Duration::from_millis(500));
assert!(backoff.delay(3).unwrap() <= Duration::from_secs(1));
assert!(backoff.delay(4).unwrap() <= Duration::from_secs(2));
}
#[test]
fn backoff_delay_capped_at_max() {
let backoff = BackoffPolicy {
factor: 2.0,
max_delay: Duration::from_secs(5),
initial_delay: Duration::from_secs(1),
};
let d = backoff.delay(10).unwrap();
assert!(d < Duration::from_secs(5)); }
#[test]
fn retry_after_disabled_by_default() {
let policy = RetryPolicy::default();
assert!(policy.retry_after_delay("5").is_none());
}
#[test]
fn retry_after_respected_when_enabled() {
let policy = RetryPolicy::builder()
.max_attempts(3)
.respect_retry_after(true)
.build();
let delay = policy.retry_after_delay("5").unwrap();
assert_eq!(delay, Duration::from_secs(5));
}
#[test]
fn retry_after_capped_at_max_delay() {
let policy = RetryPolicy::builder()
.max_attempts(3)
.respect_retry_after(true)
.max_delay(Duration::from_secs(10))
.build();
let delay = policy.retry_after_delay("60").unwrap();
assert_eq!(delay, Duration::from_secs(10));
}
#[test]
fn retry_after_invalid_ignored() {
let policy = RetryPolicy::builder()
.max_attempts(3)
.respect_retry_after(true)
.build();
assert!(policy.retry_after_delay("not-a-number").is_none());
}
#[test]
fn retry_after_http_date_parses() {
use std::time::UNIX_EPOCH;
let policy = RetryPolicy::builder()
.max_attempts(3)
.respect_retry_after(true)
.build();
let future_time = UNIX_EPOCH
+ Duration::from_secs(
std::time::SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs()
+ 3600,
); let formatted = httpdate::fmt_http_date(future_time);
let parsed = httpdate::parse_http_date(&formatted);
assert!(parsed.is_ok(), "round-trip parse failed: {formatted}");
let delay = policy.retry_after_delay(&formatted);
assert!(
delay.is_some(),
"retry_after_delay returned None for HTTP-date: {formatted}"
);
let d = delay.unwrap();
assert!(d.as_secs() > 0, "delay should be positive");
}
#[test]
fn retry_after_past_http_date_is_ignored() {
use std::time::UNIX_EPOCH;
let policy = RetryPolicy::builder()
.max_attempts(3)
.respect_retry_after(true)
.build();
let past_time = UNIX_EPOCH
+ Duration::from_secs(
std::time::SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs()
.saturating_sub(3600),
);
let formatted = httpdate::fmt_http_date(past_time);
assert!(policy.retry_after_delay(&formatted).is_none());
}
#[test]
fn body_replayable_empty() {
let body = RequestBody::Empty;
assert_eq!(
RetryPolicy::check_body_replayable(&body),
ReplayCheck::Replayable
);
}
#[test]
fn body_replayable_bytes() {
let body = RequestBody::from(bytes::Bytes::from("hello"));
assert_eq!(
RetryPolicy::check_body_replayable(&body),
ReplayCheck::Replayable
);
}
#[test]
fn body_not_replayable_stream() {
let body = RequestBody::from_stream(
futures_util::stream::empty::<std::result::Result<bytes::Bytes, crate::Error>>(),
None,
);
assert_eq!(
RetryPolicy::check_body_replayable(&body),
ReplayCheck::NotReplayable
);
}
#[test]
fn should_retry_get_with_retryable_status() {
let policy = RetryPolicy::builder().max_attempts(3).build();
let body = RequestBody::Empty;
let cause = should_retry(&policy, &Method::GET, &body, None, Some(503));
assert_eq!(cause, Some(RetryCause::Status(503)));
}
#[test]
fn should_not_retry_post_by_default() {
let policy = RetryPolicy::builder().max_attempts(3).build();
let body = RequestBody::Empty;
let cause = should_retry(&policy, &Method::POST, &body, None, Some(503));
assert_eq!(cause, None);
}
#[test]
fn should_retry_post_when_allowed() {
let policy = RetryPolicy::builder()
.max_attempts(3)
.allow_post_retry()
.build();
let body = RequestBody::Empty;
let cause = should_retry(&policy, &Method::POST, &body, None, Some(503));
assert_eq!(cause, Some(RetryCause::Status(503)));
}
#[test]
fn should_not_retry_non_retryable_status() {
let policy = RetryPolicy::builder().max_attempts(3).build();
let body = RequestBody::Empty;
let cause = should_retry(&policy, &Method::GET, &body, None, Some(200));
assert_eq!(cause, None);
}
#[test]
fn should_not_retry_stream_body() {
let policy = RetryPolicy::builder().max_attempts(3).build();
let body = RequestBody::from_stream(
futures_util::stream::empty::<std::result::Result<bytes::Bytes, crate::Error>>(),
None,
);
let cause = should_retry(&policy, &Method::GET, &body, None, Some(503));
assert_eq!(cause, None);
}
#[test]
fn should_retry_connect_error() {
let policy = RetryPolicy::builder().max_attempts(3).build();
let body = RequestBody::Empty;
let err = Error::Connect("connection refused".into());
let cause = should_retry(&policy, &Method::GET, &body, Some(&err), None);
assert!(matches!(cause, Some(RetryCause::TransportError(_))));
}
#[test]
fn should_not_retry_invalid_url() {
let policy = RetryPolicy::builder().max_attempts(3).build();
let body = RequestBody::Empty;
let err = Error::InvalidUrl("bad url".into());
let cause = should_retry(&policy, &Method::GET, &body, Some(&err), None);
assert_eq!(cause, None);
}
#[test]
#[cfg(feature = "http3")]
fn should_retry_h3_connect_error() {
let policy = RetryPolicy::builder().max_attempts(3).build();
let body = RequestBody::Empty;
let err = Error::H3Connect("QUIC handshake failed".into());
let cause = should_retry(&policy, &Method::GET, &body, Some(&err), None);
assert!(matches!(cause, Some(RetryCause::TransportError(_))));
}
#[test]
#[cfg(feature = "http3")]
fn should_not_retry_h3_connection_closed() {
let policy = RetryPolicy::builder().max_attempts(3).build();
let body = RequestBody::Empty;
let err = Error::H3ConnectionClosed("peer sent GOAWAY".into());
let cause = should_retry(&policy, &Method::GET, &body, Some(&err), None);
assert_eq!(cause, None);
}
#[test]
#[cfg(feature = "http3")]
fn should_not_retry_h3_protocol_error() {
let policy = RetryPolicy::builder().max_attempts(3).build();
let body = RequestBody::Empty;
let err = Error::H3Protocol("unexpected frame".into());
let cause = should_retry(&policy, &Method::GET, &body, Some(&err), None);
assert_eq!(cause, None);
}
#[test]
#[cfg(feature = "http3")]
fn should_not_retry_h3_stream_error() {
let policy = RetryPolicy::builder().max_attempts(3).build();
let body = RequestBody::Empty;
let err = Error::H3Stream("stream reset".into());
let cause = should_retry(&policy, &Method::GET, &body, Some(&err), None);
assert_eq!(cause, None);
}
#[test]
fn retry_cause_display() {
assert_eq!(
RetryCause::TransportError("refused".into()).to_string(),
"transport error: refused"
);
assert_eq!(RetryCause::Status(503).to_string(), "retryable status: 503");
assert_eq!(
RetryCause::Timeout("total".into()).to_string(),
"timeout: total"
);
}
#[test]
fn method_policy_add_method() {
let mut policy = MethodPolicy::default();
assert!(!policy.is_retryable(&Method::POST));
policy.add_method(Method::POST);
assert!(policy.is_retryable(&Method::POST));
}
#[test]
fn status_policy_add_status() {
let mut policy = StatusPolicy::default();
assert!(!policy.is_retryable(418));
policy.add_status(418);
assert!(policy.is_retryable(418));
}
#[test]
fn retry_policy_max_elapsed() {
let policy = RetryPolicy::builder()
.max_attempts(3)
.max_elapsed(Duration::from_secs(10))
.build();
assert_eq!(policy.max_elapsed(), Some(Duration::from_secs(10)));
}
#[test]
fn retry_policy_display_cause() {
let cause = RetryCause::Status(429);
assert_eq!(format!("{cause}"), "retryable status: 429");
}
#[test]
fn prop_backoff_delay_some_for_attempt_gt_1() {
proptest::proptest!(|(attempt in 2usize..100usize)| {
let backoff = BackoffPolicy::default();
prop_assert!(backoff.delay(attempt).is_some());
});
}
#[test]
fn prop_backoff_delay_bounded_by_max_delay() {
proptest::proptest!(|(
factor in 0.1f64..2.0f64,
initial_ms in 1u64..1000u64,
max_secs in 1u64..60u64,
attempt in 2usize..20usize,
)| {
let backoff = BackoffPolicy {
factor,
max_delay: Duration::from_secs(max_secs),
initial_delay: Duration::from_millis(initial_ms),
};
if let Some(delay) = backoff.delay(attempt) {
prop_assert!(delay <= Duration::from_secs(max_secs * 2));
}
});
}
#[test]
fn prop_method_policy_default_contains_get_head_options() {
let policy = MethodPolicy::default();
assert!(policy.is_retryable(&Method::GET));
assert!(policy.is_retryable(&Method::HEAD));
assert!(policy.is_retryable(&Method::OPTIONS));
}
#[test]
fn prop_status_policy_default_contains_expected() {
let policy = StatusPolicy::default();
assert!(policy.is_retryable(408));
assert!(policy.is_retryable(429));
assert!(policy.is_retryable(502));
assert!(policy.is_retryable(503));
assert!(policy.is_retryable(504));
}
#[test]
fn prop_retry_after_delay_returns_none_for_garbage() {
proptest::proptest!(|(input in ".{0,100}")| {
let policy = RetryPolicy::builder()
.max_attempts(3)
.respect_retry_after(true)
.build();
let _ = policy.retry_after_delay(&input);
});
}
#[test]
fn regression_fuzz_retry_crash_extreme_backoff_factor() {
let backoff = BackoffPolicy {
factor: 22.0,
max_delay: Duration::from_secs(30),
initial_delay: Duration::from_millis(500),
};
for attempt in 2..=20 {
let delay = backoff.delay(attempt);
assert!(delay.is_some());
assert!(delay.unwrap() <= backoff.max_delay);
}
}
#[test]
fn backoff_delay_nan_factor_returns_bounded_delay() {
let backoff = BackoffPolicy {
factor: f64::NAN,
max_delay: Duration::from_secs(30),
initial_delay: Duration::from_millis(500),
};
let d = backoff.delay(5).unwrap();
assert!(d <= Duration::from_secs(30));
}
#[test]
fn backoff_delay_infinite_factor_returns_bounded_delay() {
let backoff = BackoffPolicy {
factor: f64::INFINITY,
max_delay: Duration::from_secs(30),
initial_delay: Duration::from_millis(500),
};
let d = backoff.delay(5).unwrap();
assert!(d <= Duration::from_secs(30));
}
#[test]
fn backoff_delay_max_duration_does_not_panic() {
let backoff = BackoffPolicy {
factor: 2.0,
max_delay: Duration::MAX,
initial_delay: Duration::from_millis(500),
};
for attempt in [2, 5, 100] {
let d = backoff.delay(attempt).unwrap();
assert!(d <= Duration::MAX);
}
}
#[test]
fn prop_is_error_retryable_never_panics() {
proptest::proptest!(|(msg in ".{0,200}")| {
let errors = vec![
Error::Connect(msg.clone()),
Error::Io(std::sync::Arc::new(std::io::Error::other(msg.clone()))),
Error::InvalidUrl(msg.clone()),
Error::Tls(msg.clone()),
Error::Protocol(msg.clone()),
Error::Body(msg.clone()),
];
for err in &errors {
let _ = RetryPolicy::is_error_retryable(err);
}
});
}
}