pub struct BlockingRetry<B: Backoff, T, E, F: FnMut() -> Result<T, E>, SF: MaybeBlockingSleeper = DefaultBlockingSleeper, RF = fn(_: &E) -> bool, NF = fn(_: &E, _: Duration)> { /* private fields */ }
Expand description
Retry structure generated by BlockingRetryable
.
Implementations§
Source§impl<B, T, E, F, SF, RF, NF> BlockingRetry<B, T, E, F, SF, RF, NF>
impl<B, T, E, F, SF, RF, NF> BlockingRetry<B, T, E, F, SF, RF, NF>
Sourcepub fn sleep<SN: BlockingSleeper>(
self,
sleep_fn: SN,
) -> BlockingRetry<B, T, E, F, SN, RF, NF>
pub fn sleep<SN: BlockingSleeper>( self, sleep_fn: SN, ) -> BlockingRetry<B, T, E, F, SN, RF, NF>
Set the sleeper for retrying.
The sleeper should implement the BlockingSleeper
trait. The simplest way is to use a closure like Fn(Duration)
.
If not specified, we use the DefaultBlockingSleeper
.
§Examples
use anyhow::Result;
use backon::BlockingRetryable;
use backon::ExponentialBuilder;
fn fetch() -> Result<String> {
Ok("hello, world!".to_string())
}
fn main() -> Result<()> {
let retry = fetch
.retry(ExponentialBuilder::default())
.sleep(std::thread::sleep);
let content = retry.call()?;
println!("fetch succeeded: {}", content);
Ok(())
}
Sourcepub fn when<RN: FnMut(&E) -> bool>(
self,
retryable: RN,
) -> BlockingRetry<B, T, E, F, SF, RN, NF>
pub fn when<RN: FnMut(&E) -> bool>( self, retryable: RN, ) -> BlockingRetry<B, T, E, F, SF, RN, NF>
Set the conditions for retrying.
If not specified, all errors are considered retryable.
§Examples
use anyhow::Result;
use backon::BlockingRetryable;
use backon::ExponentialBuilder;
fn fetch() -> Result<String> {
Ok("hello, world!".to_string())
}
fn main() -> Result<()> {
let retry = fetch
.retry(ExponentialBuilder::default())
.when(|e| e.to_string() == "EOF");
let content = retry.call()?;
println!("fetch succeeded: {}", content);
Ok(())
}
Sourcepub fn notify<NN: FnMut(&E, Duration)>(
self,
notify: NN,
) -> BlockingRetry<B, T, E, F, SF, RF, NN>
pub fn notify<NN: FnMut(&E, Duration)>( self, notify: NN, ) -> BlockingRetry<B, T, E, F, 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::BlockingRetryable;
use backon::ExponentialBuilder;
fn fetch() -> Result<String> {
Ok("hello, world!".to_string())
}
fn main() -> Result<()> {
let retry = fetch.retry(ExponentialBuilder::default()).notify(
|err: &anyhow::Error, dur: Duration| {
println!("retrying error {:?} with sleeping {:?}", err, dur);
},
);
let content = retry.call()?;
println!("fetch succeeded: {}", content);
Ok(())
}
Auto Trait Implementations§
impl<B, T, E, F, SF, RF, NF> Freeze for BlockingRetry<B, T, E, F, SF, RF, NF>
impl<B, T, E, F, SF, RF, NF> RefUnwindSafe for BlockingRetry<B, T, E, F, SF, RF, NF>
impl<B, T, E, F, SF, RF, NF> Send for BlockingRetry<B, T, E, F, SF, RF, NF>
impl<B, T, E, F, SF, RF, NF> Sync for BlockingRetry<B, T, E, F, SF, RF, NF>
impl<B, T, E, F, SF, RF, NF> Unpin for BlockingRetry<B, T, E, F, SF, RF, NF>
impl<B, T, E, F, SF, RF, NF> UnwindSafe for BlockingRetry<B, T, E, F, SF, RF, NF>
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