Struct kwap::retry::RetryTimer
source · [−]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
sourceimpl<C: Clock<T = u64>> RetryTimer<C>
impl<C: Clock<T = u64>> RetryTimer<C>
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.
Trait Implementations
sourceimpl<C: Clone + Clock<T = u64>> Clone for RetryTimer<C>
impl<C: Clone + Clock<T = u64>> Clone for RetryTimer<C>
sourcefn clone(&self) -> RetryTimer<C>
fn clone(&self) -> RetryTimer<C>
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
impl<C: Copy + Clock<T = u64>> Copy for RetryTimer<C>
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
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more