Trait backon::Retryable

source ·
pub trait Retryable<B: BackoffBuilder, T, E, Fut: Future<Output = Result<T, E>>, FutureFn: FnMut() -> Fut> {
    // Required method
    fn retry(self, builder: &B) -> Retry<B::Backoff, T, E, Fut, FutureFn> ;
}
Expand description

Retryable will add retry support for functions that produces a futures with results.

That means all types that implement FnMut() -> impl Future<Output = Result<T, E>> will be able to use retry.

For example:

  • Functions without extra args:
async fn fetch() -> Result<String> {
    Ok(reqwest::get("https://www.rust-lang.org").await?.text().await?)
}
  • Closures
|| async {
    let x = reqwest::get("https://www.rust-lang.org")
        .await?
        .text()
        .await?;

    Err(anyhow::anyhow!(x))
}

§Example

use anyhow::Result;
use backon::ExponentialBuilder;
use backon::Retryable;

async fn fetch() -> Result<String> {
    Ok(reqwest::get("https://www.rust-lang.org")
        .await?
        .text()
        .await?)
}

#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<()> {
    let content = fetch.retry(&ExponentialBuilder::default()).await?;
    println!("fetch succeeded: {}", content);

    Ok(())
}

Required Methods§

source

fn retry(self, builder: &B) -> Retry<B::Backoff, T, E, Fut, FutureFn>

Generate a new retry

Implementors§

source§

impl<B, T, E, Fut, FutureFn> Retryable<B, T, E, Fut, FutureFn> for FutureFn
where B: BackoffBuilder, Fut: Future<Output = Result<T, E>>, FutureFn: FnMut() -> Fut,