pub struct RetryTransientMiddleware<T: RetryPolicy + Send + Sync + 'static, R: RetryableStrategy + Send + Sync + 'static = DefaultRetryableStrategy> { /* private fields */ }
Expand description
RetryTransientMiddleware
offers retry logic for requests that fail in a transient manner
and can be safely executed again.
Currently, it allows setting a RetryPolicy algorithm for calculating the wait_time between each request retry.
use reqwest_middleware::ClientBuilder;
use reqwest_retry::{RetryTransientMiddleware, policies::ExponentialBackoff};
use reqwest::Client;
// We create a ExponentialBackoff retry policy which implements `RetryPolicy`.
let retry_policy = ExponentialBackoff {
/// How many times the policy will tell the middleware to retry the request.
max_n_retries: 3,
max_retry_interval: std::time::Duration::from_millis(30),
min_retry_interval: std::time::Duration::from_millis(100),
backoff_exponent: 2,
};
let retry_transient_middleware = RetryTransientMiddleware::new_with_policy(retry_policy);
let client = ClientBuilder::new(Client::new()).with(retry_transient_middleware).build();
§Note
This middleware always errors when given requests with streaming bodies, before even executing
the request. When this happens you’ll get an Error::Middleware
with the message
‘Request object is not clonable. Are you passing a streaming body?’.
Some workaround suggestions:
- If you can fit the data in memory, you can instead build static request bodies e.g. with
Body
’sFrom<String>
orFrom<Bytes>
implementations. - You can wrap this middleware in a custom one which skips retries for streaming requests.
- You can write a custom retry middleware that builds new streaming requests from the data source directly, avoiding the issue of streaming requests not being clonable.
Implementations§
Source§impl<T: RetryPolicy + Send + Sync> RetryTransientMiddleware<T, DefaultRetryableStrategy>
impl<T: RetryPolicy + Send + Sync> RetryTransientMiddleware<T, DefaultRetryableStrategy>
Sourcepub fn new_with_policy(retry_policy: T) -> Self
pub fn new_with_policy(retry_policy: T) -> Self
Construct RetryTransientMiddleware
with a retry_policy.
Source§impl<T, R> RetryTransientMiddleware<T, R>
impl<T, R> RetryTransientMiddleware<T, R>
Sourcepub fn new_with_policy_and_strategy(
retry_policy: T,
retryable_strategy: R,
) -> Self
pub fn new_with_policy_and_strategy( retry_policy: T, retryable_strategy: R, ) -> Self
Construct RetryTransientMiddleware
with a retry_policy and retryable_strategy.
Trait Implementations§
Source§impl<T, R> Middleware for RetryTransientMiddleware<T, R>
impl<T, R> Middleware for RetryTransientMiddleware<T, R>
Source§fn handle<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
req: Request,
extensions: &'life1 mut Extensions,
next: Next<'life2>,
) -> Pin<Box<dyn Future<Output = Result<Response>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn handle<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
req: Request,
extensions: &'life1 mut Extensions,
next: Next<'life2>,
) -> Pin<Box<dyn Future<Output = Result<Response>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Invoked with a request before sending it. If you want to continue processing the request,
you should explicitly call
next.run(req, extensions)
. Read moreAuto Trait Implementations§
impl<T, R> Freeze for RetryTransientMiddleware<T, R>
impl<T, R> RefUnwindSafe for RetryTransientMiddleware<T, R>where
T: RefUnwindSafe,
R: RefUnwindSafe,
impl<T, R> Send for RetryTransientMiddleware<T, R>
impl<T, R> Sync for RetryTransientMiddleware<T, R>
impl<T, R> Unpin for RetryTransientMiddleware<T, R>
impl<T, R> UnwindSafe for RetryTransientMiddleware<T, R>where
T: UnwindSafe,
R: UnwindSafe,
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
Mutably borrows from an owned value. Read more