pub struct RetryPolicy {
pub max_attempts: usize,
pub strategy: RetryStrategy,
}Expand description
Configuration for automatic retry behaviour with backoff.
Construct via the factory helpers RetryPolicy::fixed,
RetryPolicy::exponential, or RetryPolicy::linear, then execute an
async closure with RetryPolicy::retry.
§Examples
use std::time::Duration;
use tokio_prompt_orchestrator::enhanced::RetryPolicy;
let policy = RetryPolicy::exponential(3, Duration::from_millis(100));
let result: Result<String, String> = policy.retry(|| async {
// replace with a real async call
Err("transient".to_string())
}).await;
// All 3 attempts failed
assert!(result.is_err());Fields§
§max_attempts: usizeMaximum number of attempts (including the first try).
strategy: RetryStrategyBackoff strategy applied between consecutive attempts.
Implementations§
Source§impl RetryPolicy
impl RetryPolicy
Sourcepub fn fixed(max_attempts: usize, delay: Duration) -> Self
pub fn fixed(max_attempts: usize, delay: Duration) -> Self
Create a policy that waits a constant delay between attempts.
§Arguments
max_attempts— Total attempts including the first try.1means no retries.delay— Fixed pause between consecutive attempts.
§Examples
use std::time::Duration;
use tokio_prompt_orchestrator::enhanced::RetryPolicy;
let policy = RetryPolicy::fixed(3, Duration::from_millis(50));
assert_eq!(policy.max_attempts, 3);Sourcepub fn exponential(max_attempts: usize, initial_delay: Duration) -> Self
pub fn exponential(max_attempts: usize, initial_delay: Duration) -> Self
Create a policy with exponential back-off (multiplier 2×, cap 60 s).
The delay before attempt n is:
min(initial_delay × 2^(n-1), 60 s)
§Arguments
max_attempts— Total attempts including the first try.initial_delay— Delay before the second attempt.
§Examples
use std::time::Duration;
use tokio_prompt_orchestrator::enhanced::RetryPolicy;
// Delays: 100 ms, 200 ms, 400 ms
let policy = RetryPolicy::exponential(4, Duration::from_millis(100));Sourcepub fn linear(
max_attempts: usize,
initial_delay: Duration,
increment: Duration,
) -> Self
pub fn linear( max_attempts: usize, initial_delay: Duration, increment: Duration, ) -> Self
Create policy with linear backoff
Sourcepub async fn retry<F, Fut, T, E>(&self, f: F) -> Result<T, E>
pub async fn retry<F, Fut, T, E>(&self, f: F) -> Result<T, E>
Execute a fallible async closure, retrying on error according to this policy.
The closure f is called up to max_attempts times. Between
consecutive failures the task sleeps for the duration computed by the
configured RetryStrategy.
§Arguments
f— AFnMutthat returns aFuture<Output = Result<T, E>>.
§Returns
Ok(value) from the first successful attempt, or Err(last_error) if
all attempts fail.
§Examples
use std::time::Duration;
use tokio_prompt_orchestrator::enhanced::RetryPolicy;
let policy = RetryPolicy::fixed(3, Duration::from_millis(10));
let result: Result<&str, &str> = policy.retry(|| async { Ok("done") }).await;
assert_eq!(result, Ok("done"));Sourcepub async fn retry_with_details<F, Fut, T, E>(&self, f: F) -> RetryResult<T, E>
pub async fn retry_with_details<F, Fut, T, E>(&self, f: F) -> RetryResult<T, E>
Execute with retries, returning detailed result
Sourcepub fn is_retryable<E>(&self, _error: &E) -> bool
pub fn is_retryable<E>(&self, _error: &E) -> bool
Check if error is retryable (can be customized)
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 moreAuto 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
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,
§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
§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>
Source§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