7 releases

0.3.2 Oct 7, 2023
0.3.1 Aug 1, 2023
0.3.0 Jul 14, 2023
0.2.1 Nov 17, 2022
0.1.1 Jun 18, 2022

#387 in HTTP client


Used in 2 crates (via tfc-toolset-extras)

MIT/Apache

10KB
137 lines

surf-retry

A retry middleware for surf

Install

With cargo add installed :

cargo add surf-retry

Documentation

Example

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

use std::time::Duration;

#[async_std::main]
async fn main() -> surf::Result<()> {
    let req = Request::new(Method::Get, Url::parse("https://example.api")?);
    let client = Client::new().with(RetryMiddleware::default());
    let res = client.send(req).await?;
    Ok(())
}

lib.rs:

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(())
}

Dependencies

~11–24MB
~342K SLoC