Skip to main content

RetryConfig

Struct RetryConfig 

Source
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§

Source§

impl RetryConfig

Source

pub fn validate(&self) -> Result<(), BaseError>

验证请求级重试策略。

max_retries == 0 表示显式禁用重试,此时 retry_on 与 backoff_ms 不参与发送。

Trait Implementations§

Source§

impl Clone for RetryConfig

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for RetryConfig

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for RetryConfig

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more