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

#616 in HTTP client

Download history 2/week @ 2024-11-17 6/week @ 2024-11-24 6/week @ 2024-12-01 46/week @ 2024-12-08 17/week @ 2024-12-15 210/week @ 2025-01-12 364/week @ 2025-01-19 1071/week @ 2025-01-26 3604/week @ 2025-02-02 2975/week @ 2025-02-09 2213/week @ 2025-02-16 2376/week @ 2025-02-23 3511/week @ 2025-03-02

11,282 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

~12–24MB
~362K SLoC