pub struct RetryPolicy {
pub max_attempts: u32,
pub base_delay: Duration,
pub kind: RetryKind,
}Expand description
Configurable retry policy with exponential backoff or constant interval.
Fields§
§max_attempts: u32Maximum number of attempts (including the first).
base_delay: DurationBase delay for the first retry.
kind: RetryKindWhether to use exponential or constant delay.
Implementations§
Source§impl RetryPolicy
impl RetryPolicy
Sourcepub fn exponential(
max_attempts: u32,
base_ms: u64,
) -> Result<Self, AgentRuntimeError>
pub fn exponential( max_attempts: u32, base_ms: u64, ) -> Result<Self, AgentRuntimeError>
Sourcepub fn constant(
max_attempts: u32,
delay_ms: u64,
) -> Result<Self, AgentRuntimeError>
pub fn constant( max_attempts: u32, delay_ms: u64, ) -> Result<Self, AgentRuntimeError>
Create a constant (fixed-interval) retry policy.
Every retry waits exactly delay_ms milliseconds regardless of attempt
number, unlike exponential which doubles the delay each time.
§Returns
Ok(RetryPolicy)— on successErr(AgentRuntimeError::Orchestration)— ifmax_attempts == 0ordelay_ms == 0
Sourcepub fn none() -> Self
pub fn none() -> Self
Create a no-retry policy (single attempt, no delay).
Useful for one-shot operations or when the caller manages retry logic externally.
Sourcepub fn is_none(&self) -> bool
pub fn is_none(&self) -> bool
Return true if this policy makes at most one attempt with no delay.
Equivalent to max_attempts == 1 && base_delay == Duration::ZERO.
Sourcepub fn with_max_attempts(self, n: u32) -> Result<Self, AgentRuntimeError>
pub fn with_max_attempts(self, n: u32) -> Result<Self, AgentRuntimeError>
Sourcepub fn max_attempts(&self) -> u32
pub fn max_attempts(&self) -> u32
Return the configured maximum number of attempts.
Sourcepub fn is_no_retry(&self) -> bool
pub fn is_no_retry(&self) -> bool
Return true if this policy performs no retries (max_attempts ≤ 1).
Useful for short-circuiting retry logic in hot paths.
Sourcepub fn will_retry_at_all(&self) -> bool
pub fn will_retry_at_all(&self) -> bool
Return true if this policy allows at least one retry (max_attempts > 1).
Complement of is_no_retry.
Sourcepub fn is_exponential(&self) -> bool
pub fn is_exponential(&self) -> bool
Return true if this policy uses exponential back-off between retries.
Sourcepub fn is_constant(&self) -> bool
pub fn is_constant(&self) -> bool
Return true if this policy uses a constant (fixed-interval) delay between retries.
Sourcepub fn base_delay_ms(&self) -> u64
pub fn base_delay_ms(&self) -> u64
Return the configured base delay in milliseconds.
For constant policies this equals every per-retry delay. For exponential policies this is the delay before the first retry.
Sourcepub fn first_delay_ms(&self) -> u64
pub fn first_delay_ms(&self) -> u64
Return the delay before the first retry in milliseconds.
Alias for base_delay_ms; the name communicates intent more clearly at
call sites that only care about the first-retry delay.
Sourcepub fn is_last_attempt(&self, attempt: u32) -> bool
pub fn is_last_attempt(&self, attempt: u32) -> bool
Return true if attempt is the last allowed attempt for this policy.
attempt is 1-indexed: attempt == max_attempts means no more retries.
Sourcepub fn max_total_delay_ms(&self) -> u64
pub fn max_total_delay_ms(&self) -> u64
Return the sum of all per-attempt delays across all attempts, in milliseconds.
For exponential policies each attempt’s delay is capped at
MAX_RETRY_DELAY. For constant policies every attempt uses
base_delay_ms.
Sourcepub fn delay_sum_ms(&self, n: u32) -> u64
pub fn delay_sum_ms(&self, n: u32) -> u64
Return the sum of delays for the first n attempts, in milliseconds.
If n > max_attempts, only max_attempts delays are summed.
Sourcepub fn avg_delay_ms(&self) -> u64
pub fn avg_delay_ms(&self) -> u64
Return the average delay per attempt in milliseconds.
Returns 0 for policies with no delay (e.g. RetryPolicy::none()).
Sourcepub fn backoff_factor(&self) -> f64
pub fn backoff_factor(&self) -> f64
Return the effective backoff factor per attempt.
Returns 2.0 for exponential policies and 1.0 for constant policies.
Sourcepub fn with_base_delay_ms(self, ms: u64) -> Result<Self, AgentRuntimeError>
pub fn with_base_delay_ms(self, ms: u64) -> Result<Self, AgentRuntimeError>
Return a copy of this policy with the base delay changed to ms milliseconds.
§Errors
Returns Err if ms == 0.
Sourcepub fn delay_ms_for(&self, attempt: u32) -> u64
pub fn delay_ms_for(&self, attempt: u32) -> u64
Return the delay for attempt in whole milliseconds.
Convenience wrapper around delay_for for use in logging and metrics
where a u64 is easier to handle than a Duration.
Sourcepub fn total_max_delay_ms(&self) -> u64
pub fn total_max_delay_ms(&self) -> u64
Return the total maximum delay in milliseconds across all retry attempts.
Sums delay_for(attempt) for every attempt from 1 to max_attempts.
Useful for estimating worst-case latency budgets.
Sourcepub fn attempts_remaining(&self, attempt: u32) -> u32
pub fn attempts_remaining(&self, attempt: u32) -> u32
Return the number of attempts still available after attempt have been made.
Returns 0 once the budget is exhausted (attempt >= max_attempts).
Sourcepub fn attempts_budget_used(&self, attempt: u32) -> f64
pub fn attempts_budget_used(&self, attempt: u32) -> f64
Return the fraction of the retry budget consumed after attempt attempts.
Computed as attempt / max_attempts, clamped to [0.0, 1.0].
Returns 1.0 when max_attempts is zero (budget fully consumed by
definition).
Useful for surfacing “how far through the retry budget are we” in dashboards and progress logs.
Sourcepub fn max_delay_ms(&self) -> u64
pub fn max_delay_ms(&self) -> u64
Return the maximum delay for any single attempt in milliseconds.
For Exponential policies this is the delay for the last attempt
(which may be capped by MAX_RETRY_DELAY). For Constant policies it
equals the base delay. Returns 0 for a none() policy.
Sourcepub fn can_retry(&self, attempt: u32) -> bool
pub fn can_retry(&self, attempt: u32) -> bool
Return true if another attempt is permitted after attempt failures.
attempt is the number of attempts already made (0-based: 0 means
no attempt has been made yet). Returns false once the budget is
exhausted (i.e. attempt >= max_attempts).
Sourcepub fn delay_for(&self, attempt: u32) -> Duration
pub fn delay_for(&self, attempt: u32) -> Duration
Compute the delay before the given attempt number (1-based).
RetryKind::Exponential:base_delay * 2^(attempt-1), capped atMAX_RETRY_DELAY.RetryKind::Constant: always returnsbase_delay.
Sourcepub fn is_bounded(&self) -> bool
pub fn is_bounded(&self) -> bool
Return true if the policy has a finite retry limit.
A policy with max_attempts < u32::MAX is considered bounded.
In practice all policies created via the public constructors are bounded.
Sourcepub fn remaining_wait_budget_ms(&self, attempts_done: u32) -> u64
pub fn remaining_wait_budget_ms(&self, attempts_done: u32) -> u64
Return the remaining wait budget in milliseconds after attempts_done
have been completed.
Computed as max_total_delay_ms().saturating_sub(delay_sum_ms(attempts_done)).
Returns 0 when attempts_done equals or exceeds max_attempts.
Sourcepub fn max_single_delay_ms(&self) -> u64
pub fn max_single_delay_ms(&self) -> u64
Return the maximum delay a single retry attempt can incur, in milliseconds.
For exponential policies this is delay_for(max_attempts) which equals
base * 2^(max_attempts-1) capped at the global MAX_RETRY_DELAY.
For constant policies this equals the configured base delay.
Sourcepub fn covers_n_failures(&self, n: u32) -> bool
pub fn covers_n_failures(&self, n: u32) -> bool
Return true if this policy allows at least n retry attempts
(i.e. max_attempts > n).
Useful for validating that a policy can tolerate a minimum number of consecutive failures before giving up.
Trait Implementations§
Source§impl Clone for RetryPolicy
impl Clone for RetryPolicy
Source§fn clone(&self) -> RetryPolicy
fn clone(&self) -> RetryPolicy
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for RetryPolicy
impl Debug for RetryPolicy
Auto Trait Implementations§
impl Freeze for RetryPolicy
impl RefUnwindSafe for RetryPolicy
impl Send for RetryPolicy
impl Sync for RetryPolicy
impl Unpin for RetryPolicy
impl UnsafeUnpin for RetryPolicy
impl UnwindSafe for RetryPolicy
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,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
§impl<T> ToStringFallible for Twhere
T: Display,
impl<T> ToStringFallible for Twhere
T: Display,
§fn try_to_string(&self) -> Result<String, TryReserveError>
fn try_to_string(&self) -> Result<String, TryReserveError>
ToString::to_string, but without panic on OOM.