use std::time::{Duration, SystemTime};
use reqwest::StatusCode;
use reqwest::header::HeaderMap;
use super::{HARD_MAX_DELAY_MS, MILLIS_PER_SECOND};
const RETRY_AFTER_SKEW_SECS: u64 = 1;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RetryClass {
Permanent,
Transient,
RateLimited {
retry_after: Option<Duration>,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RetryKind {
Permanent,
TransientNetwork,
TransientServer,
RateLimited,
Timeout,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ErrorLayer {
Dns,
TcpConnect,
Tls,
HttpProtocol,
HttpStatus,
Application,
Unknown,
}
impl ErrorLayer {
pub fn as_str(self) -> &'static str {
match self {
Self::Dns => "dns",
Self::TcpConnect => "tcp_connect",
Self::Tls => "tls",
Self::HttpProtocol => "http_protocol",
Self::HttpStatus => "http_status",
Self::Application => "application",
Self::Unknown => "unknown",
}
}
}
impl RetryKind {
pub fn as_str(self) -> &'static str {
match self {
Self::Permanent => "permanent",
Self::TransientNetwork => "transient_network",
Self::TransientServer => "transient_server",
Self::RateLimited => "rate_limited",
Self::Timeout => "timeout",
}
}
pub fn is_retryable(self) -> bool {
!matches!(self, Self::Permanent)
}
}
pub fn classify_http_status(status: StatusCode, retry_after: Option<Duration>) -> RetryClass {
match status.as_u16() {
429 => RetryClass::RateLimited { retry_after },
408 | 500 | 502 | 504 => RetryClass::Transient,
503 => {
if retry_after.is_some() {
RetryClass::RateLimited { retry_after }
} else {
RetryClass::Transient
}
}
_ => RetryClass::Permanent,
}
}
pub(crate) fn source_chain_has_rustls(err: &(dyn std::error::Error + 'static)) -> bool {
let mut cur: Option<&(dyn std::error::Error + 'static)> = Some(err);
while let Some(e) = cur {
if e.downcast_ref::<rustls::Error>().is_some() {
return true;
}
cur = e.source();
}
false
}
pub fn classify_reqwest_error(err: &reqwest::Error) -> (crate::error::ErrorKind, ErrorLayer, bool) {
use crate::error::ErrorKind;
let tls = source_chain_has_rustls(err as &(dyn std::error::Error + 'static));
if err.is_timeout() {
let layer = if tls {
ErrorLayer::Tls
} else {
ErrorLayer::TcpConnect
};
return (ErrorKind::Timeout, layer, true);
}
if tls {
return (ErrorKind::Network, ErrorLayer::Tls, true);
}
if err.is_connect() {
return (ErrorKind::Network, ErrorLayer::TcpConnect, true);
}
if err.is_request() {
return (ErrorKind::Network, ErrorLayer::HttpProtocol, true);
}
if err.is_body() || err.is_decode() {
return (ErrorKind::Network, ErrorLayer::HttpProtocol, false);
}
(ErrorKind::Network, ErrorLayer::Unknown, false)
}
pub fn parse_retry_after(headers: &HeaderMap) -> Option<Duration> {
let raw = headers
.get(reqwest::header::RETRY_AFTER)
.and_then(|v| v.to_str().ok())?
.trim();
if raw.is_empty() {
return None;
}
if raw.bytes().all(|b| b.is_ascii_digit()) {
let secs: u64 = raw.parse().ok()?;
return Some(Duration::from_secs(
secs.min(HARD_MAX_DELAY_MS / MILLIS_PER_SECOND),
));
}
let when = httpdate::parse_http_date(raw).ok()?;
let now = SystemTime::now();
match when.duration_since(now) {
Ok(d) => Some(d.min(Duration::from_millis(HARD_MAX_DELAY_MS))),
Err(e) => {
if e.duration() <= Duration::from_secs(RETRY_AFTER_SKEW_SECS) {
Some(Duration::ZERO)
} else {
None
}
}
}
}