notoriaga / backoff-futures

A retry and backoff mechanism for `std::future::Future`

Home Page:https://docs.rs/backoff-futures/latest/backoff_futures/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

backoff-futures

DEPRECATED: see backoff::future.

crates.io

A retry and backoff mechanism for std::future::Future.

Documentation

Adding as a dependency

Manually

[dependencies]
backoff-futures = "0.3"

Usage

#![feature(async_await)]

fn isahc_error_to_backoff(err: isahc::Error) -> backoff::Error<isahc::Error> {
    match err {
        isahc::Error::Aborted | isahc::Error::Io(_) | isahc::Error::Timeout =>
            backoff::Error::Transient(err),
        _ =>
            backoff::Error::Permanent(err)
    }
}

async fn get_example_contents() -> Result<String, backoff::Error<isahc::Error>> {
    use isahc::ResponseExt;

    let mut response = isahc::get_async("https://example.org")
        .await
        .map_err(isahc_error_to_backoff)?;

    response
        .text_async()
        .await
        .map_err(|err: std::io::Error| backoff::Error::Transient(isahc::Error::Io(err)))
}

async fn get_example_contents_with_retry() -> Result<String, isahc::Error> {
    use backoff_futures::BackoffExt;

    let mut backoff = backoff::ExponentialBackoff::default();
    get_example_contents.with_backoff(&mut backoff)
        .await
        .map_err(|err| match err {
            backoff::Error::Transient(err) | backoff::Error::Permanent(err) => err
        })
}

License

MIT

About

A retry and backoff mechanism for `std::future::Future`

https://docs.rs/backoff-futures/latest/backoff_futures/


Languages

Language:Rust 100.0%