Struct toad::retry::RetryTimer
source · pub struct RetryTimer<C: Clock> { /* private fields */ }
Expand description
A non-blocking timer that allows a fixed-delay or exponential-backoff retry, that lives alongside some operation to retry.
It does not contain the work to be done (e.g. Box<fn()>
) because
we don’t have the luxury of a memory allocator :)
use embedded_time::clock::Clock;
use embedded_time::duration::Milliseconds;
use toad::retry;
fn main() {
let mut called = false;
let mut fails_once = || -> Result<(), ()> {
// ...
};
let clock = toad::std::Clock::new();
let now = || clock.try_now().unwrap();
let strategy = retry::Strategy::Delay { min: Milliseconds(1),
max: Milliseconds(2) };
let mut retry = retry::RetryTimer::new(now(), strategy, retry::Attempts(2));
while let Err(_) = fails_once() {
match nb::block!(retry.what_should_i_do(now())) {
| Ok(retry::YouShould::Retry) => continue,
| Ok(retry::YouShould::Cry) => panic!("no more attempts! it failed more than once!!"),
| Err(clock_err) => unreachable!(),
}
}
}
Implementations§
source§impl<C> RetryTimer<C>where
C: Clock,
impl<C> RetryTimer<C>where C: Clock,
sourcepub fn new(
start: Instant<C>,
strategy: Strategy,
max_attempts: Attempts
) -> Self
pub fn new( start: Instant<C>, strategy: Strategy, max_attempts: Attempts ) -> Self
Create a new retrier
sourcepub fn what_should_i_do(
&mut self,
now: Instant<C>
) -> Result<YouShould, Infallible>
pub fn what_should_i_do( &mut self, now: Instant<C> ) -> Result<YouShould, Infallible>
When the thing we keep trying fails, invoke this to tell the retrytimer “it failed again! what do I do??”
Returns nb::Error::WouldBlock
when we have not yet
waited the appropriate amount of time to retry.
sourcepub fn first_attempted_at(&self) -> Instant<C>
pub fn first_attempted_at(&self) -> Instant<C>
Get the instant this retry timer was first attempted
sourcepub fn last_attempted_at(&self) -> Instant<C>
pub fn last_attempted_at(&self) -> Instant<C>
Get the instant this retry timer was last attempted (if at all)
sourcepub fn next_attempt_at(&self) -> Instant<C>
pub fn next_attempt_at(&self) -> Instant<C>
Get the next time at which this should be retried
Trait Implementations§
source§impl<C> Clone for RetryTimer<C>where
C: Clock,
impl<C> Clone for RetryTimer<C>where C: Clock,
source§impl<C> PartialEq<RetryTimer<C>> for RetryTimer<C>where
C: Clock,
impl<C> PartialEq<RetryTimer<C>> for RetryTimer<C>where C: Clock,
impl<C> Copy for RetryTimer<C>where C: Clock,
impl<C> Eq for RetryTimer<C>where C: Clock,
Auto Trait Implementations§
impl<C> RefUnwindSafe for RetryTimer<C>
impl<C> Send for RetryTimer<C>
impl<C> Sync for RetryTimer<C>
impl<C> Unpin for RetryTimer<C>
impl<C> UnwindSafe for RetryTimer<C>
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, A, TF, T> Sequence<F, A, TF> for T
impl<F, A, TF, T> Sequence<F, A, TF> for T
source§fn sequence<Ap>(self) -> <Ap as HKT1>::T<<F as HKT1>::T<A>>where
Self: Sized + Traversable<F, <Ap as HKT1>::T<A>, A, TF> + Foldable<F, <Ap as HKT1>::T<A>>,
Ap: HKT1,
<Ap as HKT1>::T<A>: Applicative<Ap, A> + ApplyOnce<Ap, A>,
<Ap as HKT1>::T<TF>: Applicative<Ap, TF> + ApplyOnce<Ap, TF>,
<Ap as HKT1>::T<<F as HKT1>::T<A>>: Applicative<Ap, <F as HKT1>::T<A>> + ApplyOnce<Ap, <F as HKT1>::T<A>>,
F: HKT1<T<<Ap as HKT1>::T<A>> = Self>,
fn sequence<Ap>(self) -> <Ap as HKT1>::T<<F as HKT1>::T<A>>where Self: Sized + Traversable<F, <Ap as HKT1>::T<A>, A, TF> + Foldable<F, <Ap as HKT1>::T<A>>, Ap: HKT1, <Ap as HKT1>::T<A>: Applicative<Ap, A> + ApplyOnce<Ap, A>, <Ap as HKT1>::T<TF>: Applicative<Ap, TF> + ApplyOnce<Ap, TF>, <Ap as HKT1>::T<<F as HKT1>::T<A>>: Applicative<Ap, <F as HKT1>::T<A>> + ApplyOnce<Ap, <F as HKT1>::T<A>>, F: HKT1<T<<Ap as HKT1>::T<A>> = Self>,
See
Sequence