pub struct RetryConfig {
pub max_retries: u32,
pub retry_on: Vec<u16>,
pub backoff_ms: u64,
pub retry_non_idempotent: bool,
pub total_budget_ms: u64,
pub jitter_percent: u8,
}Expand description
请求级重试策略配置(L-4)。
对临时性失败(连接错误、可重试的 5xx 等)按指数退避自动重试。
默认不重试——只有显式调用 RequestBuilder::retry 才启用。
注意:当前仅实现「重试 + 指数退避 + 抖动」。熔断(circuit breaker)尚未实现, 如需熔断请在调用方或网关层处理。
非幂等方法(POST/PATCH)默认不参与重试——即使启用了重试也只发送一次;
需要重试时须显式设置 RetryConfig::retry_non_idempotent 为 true,
由调用方自行承担重复写入(重复扣款/重复插入)的风险。
重试等待受 RetryConfig::total_budget_ms 总预算钳制,避免长退避把单次
send() 拖成不可控的长尾;另有 RetryConfig::jitter_percent 抖动去同步
并发重试。本配置只在调用方显式调用 RequestBuilder::retry 时生效,
应用层应另外配置 transport 的 request_timeout
(AxumTransportConfig::request_timeout,默认 None)作为最终兜底。
§示例
use yang_base::http::RetryConfig;
let cfg = RetryConfig {
max_retries: 3,
retry_on: vec![502, 503, 504],
backoff_ms: 100, // 第 n 次重试前等待 backoff_ms * 2^(n-1) 毫秒
retry_non_idempotent: false, // POST/PATCH 不重试
total_budget_ms: 30_000, // 单次 send() 内全部退避等待的总预算
jitter_percent: 25, // 退避抖动幅度 ±25%
};Fields§
§max_retries: u32最大重试次数(不含首次请求)。0 等价于不重试。
retry_on: Vec<u16>命中这些 HTTP 状态码时重试(如 [502, 503, 504])。
backoff_ms: u64初始退避毫秒数,按 backoff_ms * 2^attempt 指数增长。
retry_non_idempotent: bool是否允许非幂等方法(POST/PATCH)重试。默认 false。
默认关闭时,非幂等请求即使启用了重试也只发送一次,避免
「服务端已处理但响应超时/连接中断」时重复扣款、重复插入。
仅当调用方确认接口幂等(如带幂等键)时才可置为 true。
total_budget_ms: u64单次 send() 内全部重试等待的总时间预算(毫秒)。
每次退避等待取 min(抖动后退避, 剩余预算);预算耗尽后不再等待,
直接返回最后一次请求的结果。必须大于 0(禁止「无预算」后门),
上限 300_000 毫秒(MAX_TOTAL_BUDGET_MS)。
jitter_percent: u8单次退避的抖动幅度(百分比 0..=100)。0 表示不抖动。
抖动用于打散并发请求的同步重试(惊群):实际上限为初始退避的
±jitter_percent%,取 SystemTime 纳秒 + 进程号 + 重试序号混合的
非密码学熵源,不引入额外依赖。
Implementations§
Trait Implementations§
Source§impl Clone for RetryConfig
impl Clone for RetryConfig
Source§impl Debug for RetryConfig
impl Debug for RetryConfig
Auto Trait Implementations§
impl Forget for RetryConfig
impl Freeze for RetryConfig
impl Move for RetryConfig
impl RefUnwindSafe for RetryConfig
impl Send for RetryConfig
impl Sync for RetryConfig
impl Unpin for RetryConfig
impl UnsafeUnpin for RetryConfig
impl UnwindSafe for RetryConfig
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more