Crate surf_retry

Crate surf_retry 

Source
Expand description

A surf middleware that handles request retry logic

§Example

use surf_retry::{ExponentialBackoff, RetryMiddleware};
use surf_governor::GovernorMiddleware;
use surf::{Client, Request, http::Method};
use url::Url;

#[async_std::main]
async fn main() -> surf::Result<()> {
    let req = Request::new(Method::Get, Url::parse("https://example.api")?);
    // Construct the retry middleware with max retries set to 3, exponential backoff also set to a max of 3, and a fallback interval of 1 second
    let retry = RetryMiddleware::new(
       3,
       ExponentialBackoff::builder().build_with_max_retries(3),
       1,
       );
    // Construct Surf client with the retry middleware and a limit of 1 request per second to force a retry
    let client = Client::new().with(retry).with(GovernorMiddleware::per_second(1)?);
    let res = client.send(req).await?;
    Ok(())
}

Structs§

ExponentialBackoff
Exponential backoff with optional jitter.
RetryMiddleware
The middleware is constructed with settings to handle a few different situations.

Traits§

RetryPolicy
A policy for deciding whether and when to retry.