Skip to main content

system_clock/
system_clock.rs

1//! Using the built-in [`SystemClock`] (requires the `std` feature) to enforce a
2//! real wall-clock time budget on retries.
3//!
4//! Run with:
5//!
6//! ```text
7//! cargo run --example system_clock --features std
8//! ```
9
10use std::ops::ControlFlow;
11use std::time::Duration;
12
13use sisyphus::{retry_sync, Constant, PolicyExt, RetryError, SystemClock};
14
15fn main() {
16    // Poll every 20ms, but never spend more than 200ms of real time trying.
17    let policy = Constant::new(Duration::from_millis(20))
18        .max_elapsed_time(SystemClock, Duration::from_millis(200));
19
20    let mut attempt = 0u32;
21    let result: Result<(), RetryError<u32>> = retry_sync(
22        policy,
23        || {
24            attempt += 1;
25            ControlFlow::Continue(attempt) // never succeeds; rely on the budget
26        },
27        std::thread::sleep,
28    );
29
30    match result {
31        Ok(()) => unreachable!(),
32        Err(RetryError::Exhausted(last)) => {
33            println!("real-time budget exhausted after {last} attempts");
34        }
35    }
36}