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,

source

pub fn new( start: Instant<C>, strategy: Strategy, max_attempts: Attempts ) -> Self

Create a new retrier

source

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.

source

pub fn first_attempted_at(&self) -> Instant<C>

Get the instant this retry timer was first attempted

source

pub fn last_attempted_at(&self) -> Instant<C>

Get the instant this retry timer was last attempted (if at all)

source

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,

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<C: Debug + Clock> Debug for RetryTimer<C>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<C> PartialEq<RetryTimer<C>> for RetryTimer<C>where C: Clock,

source§

fn eq(&self, other: &Self) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<C> Copy for RetryTimer<C>where C: Clock,

source§

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> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same<T> for T

§

type Output = T

Should always be Self
source§

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>,

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V