use std::time::Duration;
use crate::client::Client;
use crate::error::{Error, Result};
use crate::request::Request;
use crate::response::Response;
use crate::retry::{should_retry, RetryCause, RetryPolicy};
use crate::timeout::TimeoutPhase;
use super::drain_response_body;
async fn sleep_if_budget_allows(
policy: &RetryPolicy,
delay: Duration,
attempt: usize,
start_time: std::time::Instant,
total: Option<Duration>,
) -> Result<()> {
if let Some(max_elapsed) = policy.max_elapsed() {
let elapsed = start_time.elapsed();
match elapsed.checked_add(delay) {
Some(t) if t <= max_elapsed => {}
_ => return Err(Error::RetryBudgetExhausted { attempts: attempt }),
}
}
if let Some(total) = total {
let elapsed = start_time.elapsed();
match elapsed.checked_add(delay) {
Some(t) if t <= total => {}
_ => {
return Err(Error::Timeout {
phase: TimeoutPhase::Total,
elapsed: start_time.elapsed(),
});
}
}
}
tokio::time::sleep(delay).await;
Ok(())
}
fn has_budget(policy: &RetryPolicy, attempt: usize, start_time: std::time::Instant) -> bool {
if attempt >= policy.max_attempts() {
return false;
}
if let Some(max_elapsed) = policy.max_elapsed() {
if start_time.elapsed() >= max_elapsed {
return false;
}
}
true
}
async fn dispatch_attempt(client: &Client, request: Request) -> Result<Response> {
#[cfg(feature = "redirects")]
{
Box::pin(super::redirect::send_with_redirects(client, request)).await
}
#[cfg(not(feature = "redirects"))]
{
Box::pin(super::lean::send_lean(client, request)).await
}
}
#[allow(
clippy::too_many_lines,
reason = "the retry loop keeps the attempt budget checks, the backoff computation and the replayable-body guard in one linear flow"
)]
pub(crate) async fn send_with_retry(client: &Client, request: Request) -> Result<Response> {
let body_replayable = request.body().is_replayable();
let effective_policy = request
.retry()
.cloned()
.or_else(|| client.config().retry.clone());
let policy = match effective_policy {
Some(p) if p.is_enabled() => p,
_ => {
return dispatch_attempt(client, request).await;
}
};
if !body_replayable {
return dispatch_attempt(client, request).await;
}
let saved = request.into_parts();
let saved_timeout = saved.timeout;
let start_time = std::time::Instant::now();
let mut attempt = 0usize;
loop {
attempt += 1;
if let Some(context) = saved.failure_context.as_deref() {
context.clear();
}
if attempt > policy.max_attempts() {
return Err(Error::RetryBudgetExhausted {
attempts: attempt - 1,
});
}
let elapsed = start_time.elapsed();
if let Some(max_elapsed) = policy.max_elapsed() {
if elapsed >= max_elapsed {
return Err(Error::RetryBudgetExhausted {
attempts: attempt - 1,
});
}
}
if let Some(total) = saved_timeout.as_ref().and_then(|t| t.total) {
if elapsed >= total {
return Err(Error::Timeout {
phase: TimeoutPhase::Total,
elapsed,
});
}
}
let attempt_timeout =
saved_timeout.map(|t| crate::request::RequestParts::shrink_total_deadline(&t, elapsed));
if attempt_timeout
.as_ref()
.and_then(|t| t.total)
.is_some_and(|remaining| remaining.is_zero())
{
return Err(Error::Timeout {
phase: TimeoutPhase::Total,
elapsed,
});
}
let attempt_request = saved.retry_request(attempt_timeout)?;
let result = dispatch_attempt(client, attempt_request).await;
match result {
Ok(response) => {
if response.status().is_success() {
return Ok(response);
}
let status = response.status().as_u16();
if let Some(cause) =
should_retry(&policy, &saved.method, &saved.body, None, Some(status))
{
if !has_budget(&policy, attempt, start_time) {
return Ok(response);
}
let mut resp = response;
let retry_after = resp
.headers()
.get("retry-after")
.and_then(|v| v.to_str().ok())
.map(str::to_owned);
if let Some(total) = saved_timeout.as_ref().and_then(|t| t.total) {
let dur = total.saturating_sub(start_time.elapsed());
let read = saved_timeout.as_ref().and_then(|t| t.read);
let _ =
tokio::time::timeout(dur, drain_response_body(&mut resp, read)).await;
} else {
let read = saved_timeout.as_ref().and_then(|t| t.read);
drain_response_body(&mut resp, read).await;
}
if let Some(dur) =
compute_retry_delay(&policy, &cause, attempt, retry_after.as_deref())
{
let total = saved_timeout.as_ref().and_then(|t| t.total);
sleep_if_budget_allows(&policy, dur, attempt, start_time, total).await?;
}
continue;
}
return Ok(response);
}
Err(err) => {
if let Some(cause) =
should_retry(&policy, &saved.method, &saved.body, Some(&err), None)
{
if !has_budget(&policy, attempt, start_time) {
return Err(err);
}
if let Some(dur) = compute_retry_delay(&policy, &cause, attempt, None) {
let total = saved_timeout.as_ref().and_then(|t| t.total);
sleep_if_budget_allows(&policy, dur, attempt, start_time, total).await?;
}
continue;
}
return Err(err);
}
}
}
}
fn compute_retry_delay(
policy: &RetryPolicy,
cause: &RetryCause,
attempt: usize,
retry_after: Option<&str>,
) -> Option<Duration> {
if let Some(value) = retry_after {
if let Some(delay) = policy.retry_after_delay(value) {
return Some(delay);
}
}
let base = policy.backoff_delay(attempt)?;
if matches!(cause, RetryCause::Status(429)) {
let max = policy.backoff().max_delay();
let increased = base.as_secs_f64() * 1.5;
if !increased.is_finite() || increased < 0.0 {
return Some(max);
}
let max_secs = max.as_secs_f64();
let capped = increased.min(max_secs);
return Some(Duration::try_from_secs_f64(capped.max(0.0)).unwrap_or(max));
}
Some(base)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::body::RequestBody;
use crate::request::{RequestParts, ResolvedTarget, TransportHints};
use crate::timeout::Timeout;
use bytes::Bytes;
use std::sync::Arc;
fn test_url() -> url::Url {
url::Url::parse("https://example.com/path").expect("valid test URL")
}
fn full_request() -> Request {
#[cfg(not(feature = "basic-auth"))]
use crate::auth::AuthScheme;
#[cfg(feature = "basic-auth")]
use crate::auth::{AuthScheme, BasicAuth};
#[cfg(feature = "redirects")]
use crate::redirect::RedirectPolicy;
use crate::retry::RetryPolicy;
let mut req = Request::new(http::Method::POST, test_url());
req.headers_mut().insert("x-custom", "keep").unwrap();
req.headers_mut()
.insert("content-type", "text/plain")
.unwrap();
req.set_body(RequestBody::Bytes(Bytes::from("payload")));
req.set_version(http::Version::HTTP_11);
req.set_timeout(Some(
Timeout::builder()
.pool(Duration::from_secs(1))
.connect(Duration::from_secs(2))
.write(Duration::from_secs(3))
.read(Duration::from_secs(4))
.total(Duration::from_secs(30))
.build(),
));
#[cfg(feature = "redirects")]
req.set_redirect(Some(RedirectPolicy::new(true, 7)));
#[cfg(feature = "basic-auth")]
req.set_auth(Some(AuthScheme::Basic(
BasicAuth::new("user", "pass").expect("valid auth"),
)));
#[cfg(not(feature = "basic-auth"))]
req.set_auth(Some(AuthScheme::bearer("tok").expect("valid auth")));
req.set_auth_disabled(false);
req.set_decompress(Some(false));
req.set_max_decoded_body_size(Some(1024));
req.set_max_decompression_ratio(Some(12.5));
#[cfg(feature = "proxy")]
req.set_proxy_override(crate::request::ProxyOverride::Direct);
req.set_retry(Some(RetryPolicy::default()));
req.set_transport_hints(TransportHints {
target: Some(Bytes::from("/override-target")),
sni_hostname: Some("sni.example".to_owned()),
resolved_target: None,
trace: Some(Arc::new(crate::trace::NoopTraceObserver)),
});
req.set_proxied_target(Some(
ResolvedTarget::new(["192.0.2.10:443".parse().expect("valid address")])
.expect("non-empty target"),
));
req
}
#[test]
fn into_request_round_trip_preserves_every_field() {
let req = full_request();
let expected_method = req.method().clone();
let expected_url = req.url().clone();
let expected_headers = req.headers().clone();
let expected_version = req.version();
let expected_timeout = req.timeout().copied();
#[cfg(feature = "redirects")]
let expected_redirect = req.redirect().cloned();
let expected_auth = req.auth().cloned();
let expected_auth_disabled = req.is_auth_disabled();
let expected_decompress = req.decompress();
let expected_max_decoded_body_size = req.max_decoded_body_size();
let expected_max_decompression_ratio = req.max_decompression_ratio();
let expected_retry = req.retry().cloned();
let expected_target = req.transport_hints().target.clone();
let expected_sni = req.transport_hints().sni_hostname.clone();
let expected_has_trace = req.transport_hints().trace.is_some();
let expected_proxied_target = req.proxied_target().cloned();
let parts = req.into_parts();
let RequestParts {
method: _,
url: _,
headers: _,
body: _,
version: _,
timeout: _,
#[cfg(feature = "redirects")]
redirect: _,
auth: _,
auth_disabled: _,
decompress: _,
max_decoded_body_size: _,
max_decompression_ratio: _,
proxy_override: _,
retry: _,
transport_hints: _,
proxied_target: _,
failure_context: _,
} = &parts;
let rebuilt = parts.into_request();
assert_eq!(rebuilt.method(), &expected_method);
assert_eq!(rebuilt.url(), &expected_url);
assert_eq!(
rebuilt.headers().get("x-custom").unwrap().to_str().unwrap(),
"keep"
);
let _ = expected_headers;
assert_eq!(rebuilt.version(), expected_version);
match (rebuilt.timeout(), expected_timeout.as_ref()) {
(None, None) => {}
(Some(a), Some(b)) => {
assert_eq!(a.pool, b.pool);
assert_eq!(a.connect, b.connect);
assert_eq!(a.write, b.write);
assert_eq!(a.read, b.read);
assert_eq!(a.total, b.total);
}
(a, b) => panic!("timeout mismatch: {a:?} vs {b:?}"),
}
#[cfg(feature = "redirects")]
assert_eq!(
rebuilt.redirect().map(|r| (r.follow, r.max_redirects)),
expected_redirect.map(|r| (r.follow, r.max_redirects))
);
assert_eq!(
rebuilt.auth().map(|a| format!("{a:?}")),
expected_auth.map(|a| format!("{a:?}"))
);
assert_eq!(rebuilt.is_auth_disabled(), expected_auth_disabled);
assert_eq!(rebuilt.decompress(), expected_decompress);
assert_eq!(
rebuilt.max_decoded_body_size(),
expected_max_decoded_body_size
);
assert_eq!(
rebuilt.max_decompression_ratio(),
expected_max_decompression_ratio
);
#[cfg(feature = "proxy")]
assert!(matches!(
rebuilt.proxy_override(),
crate::request::ProxyOverride::Direct
));
assert_eq!(rebuilt.retry().is_some(), expected_retry.is_some());
assert_eq!(rebuilt.transport_hints().target, expected_target);
assert_eq!(rebuilt.transport_hints().sni_hostname, expected_sni);
assert_eq!(
rebuilt.transport_hints().trace.is_some(),
expected_has_trace
);
assert_eq!(rebuilt.proxied_target(), expected_proxied_target.as_ref());
match rebuilt.body() {
RequestBody::Bytes(b) => assert_eq!(b, "payload"),
other => panic!("expected bytes body, got {other:?}"),
}
}
#[test]
fn retry_request_preserves_every_override_and_shrinks_total() {
let req = full_request();
let parts = req.into_parts();
let attempt_timeout = Some(
Timeout::builder()
.pool(Duration::from_secs(1))
.total(Duration::from_secs(25))
.build(),
);
let attempt = parts.retry_request(attempt_timeout).expect("replayable");
assert_eq!(attempt.method(), &http::Method::POST);
assert_eq!(attempt.url().as_str(), "https://example.com/path");
assert_eq!(
attempt.headers().get("x-custom").unwrap().to_str().unwrap(),
"keep"
);
assert_eq!(attempt.version(), http::Version::HTTP_11);
assert_eq!(
attempt.timeout().and_then(|t| t.total),
Some(Duration::from_secs(25)),
"total deadline is replaced by the shrunk attempt budget"
);
#[cfg(feature = "redirects")]
assert!(attempt.redirect().is_some());
assert!(attempt.auth().is_some());
assert!(!attempt.is_auth_disabled());
assert_eq!(attempt.decompress(), Some(false));
assert_eq!(attempt.max_decoded_body_size(), Some(1024));
assert_eq!(attempt.max_decompression_ratio(), Some(12.5));
#[cfg(feature = "proxy")]
assert!(matches!(
attempt.proxy_override(),
crate::request::ProxyOverride::Direct
));
assert!(attempt.retry().is_some());
assert_eq!(
attempt.transport_hints().target.as_deref(),
Some(b"/override-target".as_slice())
);
assert_eq!(
attempt.transport_hints().sni_hostname.as_deref(),
Some("sni.example")
);
assert!(attempt.transport_hints().trace.is_some());
let expected_proxied_target =
ResolvedTarget::new(["192.0.2.10:443".parse().expect("valid address")])
.expect("non-empty target");
assert_eq!(attempt.proxied_target(), Some(&expected_proxied_target));
match attempt.body() {
RequestBody::Bytes(b) => assert_eq!(b, "payload"),
other => panic!("expected replayed bytes, got {other:?}"),
}
}
#[test]
fn retry_request_rejects_one_shot_stream() {
let mut req = Request::new(http::Method::POST, test_url());
req.set_body(RequestBody::from_stream(
futures_util::stream::empty::<crate::error::Result<Bytes>>(),
None,
));
let parts = req.into_parts();
let err = parts.retry_request(None).unwrap_err();
assert!(
matches!(err, Error::BodyNotReplayableForRetry),
"unexpected error: {err:?}"
);
}
#[test]
fn retry_preserves_auth_disabled_state() {
let mut req = Request::new(http::Method::GET, test_url());
req.set_auth_disabled(true);
let parts = req.into_parts();
let attempt = parts.retry_request(None).expect("empty body replays");
assert!(attempt.is_auth_disabled());
assert!(attempt.auth().is_none());
}
#[test]
fn shrink_total_deadline_subtracts_elapsed() {
let t = Timeout::builder()
.pool(Duration::from_secs(1))
.total(Duration::from_secs(10))
.build();
let shrunk = RequestParts::shrink_total_deadline(&t, Duration::from_secs(3));
assert_eq!(shrunk.total, Some(Duration::from_secs(7)));
assert_eq!(shrunk.pool, Some(Duration::from_secs(1)));
let saturated = RequestParts::shrink_total_deadline(&t, Duration::from_secs(20));
assert_eq!(saturated.total, Some(Duration::ZERO));
}
#[test]
fn compute_retry_delay_429_with_max_duration_does_not_panic() {
use crate::retry::RetryPolicy;
let policy = RetryPolicy::builder()
.max_attempts(5)
.max_delay(Duration::MAX)
.initial_delay(Duration::from_millis(500))
.build();
let d = compute_retry_delay(&policy, &RetryCause::Status(429), 5, None).unwrap();
assert!(d <= Duration::MAX);
}
}