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

#327 in HTTP client

Download history 40/week @ 2023-12-31 1/week @ 2024-01-07 5/week @ 2024-02-04 19/week @ 2024-02-18 83/week @ 2024-02-25 26/week @ 2024-03-03 21/week @ 2024-03-10 8/week @ 2024-03-17 78/week @ 2024-03-24 39/week @ 2024-03-31

149 downloads per month
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–26MB
~384K SLoC