Skip to main content

Timeout

Struct Timeout 

Source
pub struct Timeout { /* private fields */ }
Expand description

A reusable timeout budget that is not yet pinned to a timeline.

A Timeout is just a length (in your chosen monotonic unit). Configure it once and call start per operation to get a Deadline.

Implementations§

Source§

impl Timeout

Source

pub const fn new(budget: u64) -> Self

Creates a timeout with the given budget (its length).

Examples found in repository?
examples/basic.rs (line 11)
9fn main() {
10    // A 1-second budget for the whole operation.
11    let policy = Timeout::new(1_000);
12
13    // Pretend the operation starts at t = 0.
14    let deadline: Deadline = policy.start(0);
15    println!(
16        "budget: {} ms, expires at t = {}",
17        policy.budget(),
18        deadline.expiry()
19    );
20
21    // A fake monotonic clock and a fixed retry delay.
22    let mut now = 0;
23    let attempt_cost = 250; // each attempt + wait advances the clock by this much
24
25    for attempt in 1.. {
26        match deadline.check(now) {
27            None => {
28                println!(
29                    "t = {now}: deadline expired, giving up after {} attempts",
30                    attempt - 1
31                );
32                break;
33            }
34            Some(remaining) => {
35                // Never wait longer than the time left in the budget.
36                let wait = deadline.clamp(now, attempt_cost);
37                println!("t = {now}: attempt {attempt} ({remaining} ms left, waiting {wait} ms)");
38                now += attempt_cost;
39            }
40        }
41    }
42}
Source

pub const fn budget(&self) -> u64

The budget (length) of this timeout.

Examples found in repository?
examples/basic.rs (line 17)
9fn main() {
10    // A 1-second budget for the whole operation.
11    let policy = Timeout::new(1_000);
12
13    // Pretend the operation starts at t = 0.
14    let deadline: Deadline = policy.start(0);
15    println!(
16        "budget: {} ms, expires at t = {}",
17        policy.budget(),
18        deadline.expiry()
19    );
20
21    // A fake monotonic clock and a fixed retry delay.
22    let mut now = 0;
23    let attempt_cost = 250; // each attempt + wait advances the clock by this much
24
25    for attempt in 1.. {
26        match deadline.check(now) {
27            None => {
28                println!(
29                    "t = {now}: deadline expired, giving up after {} attempts",
30                    attempt - 1
31                );
32                break;
33            }
34            Some(remaining) => {
35                // Never wait longer than the time left in the budget.
36                let wait = deadline.clamp(now, attempt_cost);
37                println!("t = {now}: attempt {attempt} ({remaining} ms left, waiting {wait} ms)");
38                now += attempt_cost;
39            }
40        }
41    }
42}
Source

pub const fn start(&self, now: u64) -> Deadline

Pins this timeout to the timeline, starting at now.

Examples found in repository?
examples/basic.rs (line 14)
9fn main() {
10    // A 1-second budget for the whole operation.
11    let policy = Timeout::new(1_000);
12
13    // Pretend the operation starts at t = 0.
14    let deadline: Deadline = policy.start(0);
15    println!(
16        "budget: {} ms, expires at t = {}",
17        policy.budget(),
18        deadline.expiry()
19    );
20
21    // A fake monotonic clock and a fixed retry delay.
22    let mut now = 0;
23    let attempt_cost = 250; // each attempt + wait advances the clock by this much
24
25    for attempt in 1.. {
26        match deadline.check(now) {
27            None => {
28                println!(
29                    "t = {now}: deadline expired, giving up after {} attempts",
30                    attempt - 1
31                );
32                break;
33            }
34            Some(remaining) => {
35                // Never wait longer than the time left in the budget.
36                let wait = deadline.clamp(now, attempt_cost);
37                println!("t = {now}: attempt {attempt} ({remaining} ms left, waiting {wait} ms)");
38                now += attempt_cost;
39            }
40        }
41    }
42}
Source§

impl Timeout

Convenience methods that read the current time from a Clock instead of taking an explicit now: u64.

Available with the core feature. Each forwards to the matching now-taking method, which remains the primitive API.

Source

pub fn start_now<C: Clock>(&self, clock: &C) -> Deadline

Like start, reading the start instant from clock.

use reliakit_timeout::Timeout;
use reliakit_core::ManualClock;

let clock = ManualClock::new(1_000);
let deadline = Timeout::new(30_000).start_now(&clock);
clock.advance(10_000);
assert_eq!(deadline.remaining_now(&clock), 20_000);
assert!(!deadline.is_expired_now(&clock));

Trait Implementations§

Source§

impl Clone for Timeout

Source§

fn clone(&self) -> Timeout

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Copy for Timeout

Source§

impl Debug for Timeout

Source§

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

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

impl Default for Timeout

Source§

fn default() -> Timeout

Returns the “default value” for a type. Read more
Source§

impl Eq for Timeout

Source§

impl Hash for Timeout

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Timeout

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for Timeout

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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 T
where 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

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 T
where U: TryFrom<T>,

Source§

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.