pub struct RetryPolicy {
pub max_attempts: usize,
pub initial_backoff: Duration,
pub max_backoff: Duration,
pub max_elapsed_time: Duration,
pub backoff_factor: f64,
pub jitter: bool,
pub backoff_kind: BackoffKind,
pub linear_increment: Duration,
pub retry_if: Option<Arc<dyn Fn(&AppError) -> bool + Sync + Send>>,
pub on_retry: Option<Arc<dyn Fn(u32, &AppError) + Sync + Send>>,
pub jitter_seed: Option<u64>,
}Expand description
Retry policy with configurable backoff and retry predicate.
§Example
let policy = RetryPolicy::new()
.with_max_attempts(3)
.with_initial_backoff(std::time::Duration::from_millis(100));
let result = policy.execute(|| async {
Ok::<_, rskit_errors::AppError>(42)
}).await;Fields§
§max_attempts: usizeMaximum number of attempts before giving up (including the first call).
initial_backoff: DurationDelay before the first retry.
max_backoff: DurationUpper bound on any single backoff delay.
max_elapsed_time: DurationUpper bound on total retry elapsed time, including the first attempt.
backoff_factor: f64Multiplier applied on each successive retry when using exponential backoff.
jitter: boolWhether to add uniform jitter to each backoff delay.
backoff_kind: BackoffKindBackoff algorithm applied between retry attempts.
linear_increment: DurationLinear increment applied when BackoffKind::Linear is selected.
retry_if: Option<Arc<dyn Fn(&AppError) -> bool + Sync + Send>>Predicate that decides whether a given error is worth retrying. When None,
defaults to AppError::is_retryable().
on_retry: Option<Arc<dyn Fn(u32, &AppError) + Sync + Send>>Called after each failed attempt before the next backoff sleep. Arguments:
(attempt_number, error).
jitter_seed: Option<u64>Seed used to make jitter deterministic across runs.
Implementations§
Source§impl RetryPolicy
impl RetryPolicy
Sourcepub fn new() -> RetryPolicy
pub fn new() -> RetryPolicy
Create a new RetryPolicy with default settings.
Sourcepub fn from_preset(preset: RetryPreset) -> RetryPolicy
pub fn from_preset(preset: RetryPreset) -> RetryPolicy
Create a policy from a named retry preset.
Sourcepub fn fast() -> RetryPolicy
pub fn fast() -> RetryPolicy
Create a short retry loop for local tests and latency-sensitive operations.
Sourcepub fn standard() -> RetryPolicy
pub fn standard() -> RetryPolicy
Create a balanced default policy for service-to-service calls.
Sourcepub fn external_service() -> RetryPolicy
pub fn external_service() -> RetryPolicy
Create a more tolerant policy for external network dependencies.
Sourcepub fn with_max_attempts(self, n: usize) -> RetryPolicy
pub fn with_max_attempts(self, n: usize) -> RetryPolicy
Set the maximum number of attempts (including the first call).
Sourcepub fn with_initial_backoff(self, d: Duration) -> RetryPolicy
pub fn with_initial_backoff(self, d: Duration) -> RetryPolicy
Set the initial backoff delay before the first retry.
Sourcepub fn with_max_backoff(self, d: Duration) -> RetryPolicy
pub fn with_max_backoff(self, d: Duration) -> RetryPolicy
Set the upper bound on any single backoff delay.
Sourcepub fn with_max_elapsed_time(self, d: Duration) -> RetryPolicy
pub fn with_max_elapsed_time(self, d: Duration) -> RetryPolicy
Set the total elapsed-time cap for all attempts and backoff sleeps.
Sourcepub fn with_backoff_factor(self, f: f64) -> RetryPolicy
pub fn with_backoff_factor(self, f: f64) -> RetryPolicy
Set the exponential backoff multiplication factor.
Sourcepub fn with_jitter(self, enabled: bool) -> RetryPolicy
pub fn with_jitter(self, enabled: bool) -> RetryPolicy
Enable or disable uniform jitter on each backoff delay.
Sourcepub const fn with_jitter_seed(self, seed: u64) -> RetryPolicy
pub const fn with_jitter_seed(self, seed: u64) -> RetryPolicy
Set a deterministic seed for retry jitter.
Sourcepub fn with_constant_backoff(self, backoff: ConstantBackoff) -> RetryPolicy
pub fn with_constant_backoff(self, backoff: ConstantBackoff) -> RetryPolicy
Use a fixed delay for every retry attempt.
Sourcepub fn with_linear_backoff(self, backoff: LinearBackoff) -> RetryPolicy
pub fn with_linear_backoff(self, backoff: LinearBackoff) -> RetryPolicy
Use a linearly increasing retry delay.
Sourcepub fn with_retry_if(
self,
f: impl Fn(&AppError) -> bool + Send + Sync + 'static,
) -> RetryPolicy
pub fn with_retry_if( self, f: impl Fn(&AppError) -> bool + Send + Sync + 'static, ) -> RetryPolicy
Override the predicate used to decide whether an error is retryable.
Sourcepub fn with_on_retry(
self,
f: impl Fn(u32, &AppError) + Send + Sync + 'static,
) -> RetryPolicy
pub fn with_on_retry( self, f: impl Fn(u32, &AppError) + Send + Sync + 'static, ) -> RetryPolicy
Register a callback called after each failed attempt before the next backoff sleep.
Arguments passed: (attempt_number, error).
Sourcepub async fn execute<F, Fut, T>(&self, f: F) -> Result<T, RetryError>
pub async fn execute<F, Fut, T>(&self, f: F) -> Result<T, RetryError>
Execute f, retrying on retryable AppErrors according to this policy.
Returns Ok(T) on first success, or Err(RetryError) when exhausted.
Sourcepub fn backoff_delay(&self, attempt: usize) -> Duration
pub fn backoff_delay(&self, attempt: usize) -> Duration
Return the retry delay for a one-based failed attempt number.
Trait Implementations§
Source§impl Clone for RetryPolicy
impl Clone for RetryPolicy
Source§fn clone(&self) -> RetryPolicy
fn clone(&self) -> RetryPolicy
1.0.0 (const: unstable) · 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
Source§impl Default for RetryPolicy
impl Default for RetryPolicy
Source§fn default() -> RetryPolicy
fn default() -> RetryPolicy
Auto Trait Implementations§
impl !RefUnwindSafe for RetryPolicy
impl !UnwindSafe for RetryPolicy
impl Freeze for RetryPolicy
impl Send for RetryPolicy
impl Sync for RetryPolicy
impl Unpin for RetryPolicy
impl UnsafeUnpin for RetryPolicy
Blanket Implementations§
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
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 moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request