pub struct Retry<B: Backoff, T, E, Fut: Future<Output = Result<T, E>>, FutureFn: FnMut() -> Fut, SF: MaybeSleeper = DefaultSleeper, RF = fn(_: &E) -> bool, NF = fn(_: &E, _: Duration)> { /* private fields */ }
Expand description
Struct generated by Retryable
.
Implementations§
Source§impl<B, T, E, Fut, FutureFn, SF, RF, NF> Retry<B, T, E, Fut, FutureFn, SF, RF, NF>
impl<B, T, E, Fut, FutureFn, SF, RF, NF> Retry<B, T, E, Fut, FutureFn, SF, RF, NF>
Sourcepub fn sleep<SN: Sleeper>(
self,
sleep_fn: SN,
) -> Retry<B, T, E, Fut, FutureFn, SN, RF, NF> ⓘ
pub fn sleep<SN: Sleeper>( self, sleep_fn: SN, ) -> Retry<B, T, E, Fut, FutureFn, SN, RF, NF> ⓘ
Set the sleeper for retrying.
The sleeper should implement the Sleeper
trait. The simplest way is to use a closure that returns a Future<Output=()>
.
If not specified, we use the DefaultSleeper
.
use anyhow::Result;
use backon::ExponentialBuilder;
use backon::Retryable;
use std::future::ready;
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())
.sleep(|_| ready(()))
.await?;
println!("fetch succeeded: {}", content);
Ok(())
}
Sourcepub fn when<RN: FnMut(&E) -> bool>(
self,
retryable: RN,
) -> Retry<B, T, E, Fut, FutureFn, SF, RN, NF> ⓘ
pub fn when<RN: FnMut(&E) -> bool>( self, retryable: RN, ) -> Retry<B, T, E, Fut, FutureFn, SF, RN, NF> ⓘ
Set the conditions for retrying.
If not specified, all errors are considered retryable.
§Examples
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())
.when(|e| e.to_string() == "EOF")
.await?;
println!("fetch succeeded: {}", content);
Ok(())
}
Sourcepub fn notify<NN: FnMut(&E, Duration)>(
self,
notify: NN,
) -> Retry<B, T, E, Fut, FutureFn, SF, RF, NN> ⓘ
pub fn notify<NN: FnMut(&E, Duration)>( self, notify: NN, ) -> Retry<B, T, E, Fut, FutureFn, SF, RF, NN> ⓘ
Set to notify for all retry attempts.
When a retry happens, the input function will be invoked with the error and the sleep duration before pausing.
If not specified, this operation does nothing.
§Examples
use core::time::Duration;
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())
.notify(|err: &anyhow::Error, dur: Duration| {
println!("retrying error {:?} with sleeping {:?}", err, dur);
})
.await?;
println!("fetch succeeded: {}", content);
Ok(())
}
Trait Implementations§
Auto Trait Implementations§
impl<B, T, E, Fut, FutureFn, SF, RF, NF> Freeze for Retry<B, T, E, Fut, FutureFn, SF, RF, NF>
impl<B, T, E, Fut, FutureFn, SF, RF, NF> RefUnwindSafe for Retry<B, T, E, Fut, FutureFn, SF, RF, NF>where
B: RefUnwindSafe,
RF: RefUnwindSafe,
NF: RefUnwindSafe,
FutureFn: RefUnwindSafe,
SF: RefUnwindSafe,
Fut: RefUnwindSafe,
<SF as MaybeSleeper>::Sleep: RefUnwindSafe,
impl<B, T, E, Fut, FutureFn, SF, RF, NF> Send for Retry<B, T, E, Fut, FutureFn, SF, RF, NF>
impl<B, T, E, Fut, FutureFn, SF, RF, NF> Sync for Retry<B, T, E, Fut, FutureFn, SF, RF, NF>
impl<B, T, E, Fut, FutureFn, SF, RF, NF> Unpin for Retry<B, T, E, Fut, FutureFn, SF, RF, NF>
impl<B, T, E, Fut, FutureFn, SF, RF, NF> UnwindSafe for Retry<B, T, E, Fut, FutureFn, SF, RF, NF>where
B: UnwindSafe,
RF: UnwindSafe,
NF: UnwindSafe,
FutureFn: UnwindSafe,
SF: UnwindSafe,
Fut: UnwindSafe,
<SF as MaybeSleeper>::Sleep: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<F> IntoFuture for Fwhere
F: Future,
impl<F> IntoFuture for Fwhere
F: Future,
Source§type IntoFuture = F
type IntoFuture = F
Which kind of future are we turning this into?
Source§fn into_future(self) -> <F as IntoFuture>::IntoFuture
fn into_future(self) -> <F as IntoFuture>::IntoFuture
Creates a future from a value. Read more