pub struct RetryTimer<C: Clock<T = u64>> { /* 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 kwap::retry;

fn main() {
  let mut called = false;
  let mut fails_once = || -> Result<(), ()> {
    // ...
  };

  let clock = kwap::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

Create a new retrier

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.

Check if the strategy says an appropriate time has passed

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

Should always be Self

The resulting type after obtaining ownership.

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

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.