Skip to main content

AdaptiveTimeout

Struct AdaptiveTimeout 

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

Computes adaptive timeouts based on observed latency quantiles.

For each destination, queries the tracker for a high quantile (default: P99.99), applies a safety factor and exponential backoff, clamps between floor and ceiling, and takes the maximum across all destinations.

Falls back to pure exponential backoff when histogram data is insufficient.

§Example

use std::time::{Duration, Instant};
use adaptive_timeout::{AdaptiveTimeout, LatencyTracker};

let now = Instant::now();
let mut tracker = LatencyTracker::<u32, Instant>::default();
let timeout = AdaptiveTimeout::default();

// No data yet — falls back to exponential backoff (min_timeout).
let t = timeout.select_timeout(&mut tracker, &[1u32], 1, now);
assert_eq!(t, Duration::from_millis(250));

Implementations§

Source§

impl AdaptiveTimeout

Source

pub fn new(config: TimeoutConfig) -> Self

Creates a new AdaptiveTimeout with the given configuration.

Source

pub fn select_timeout<'a, D, I, H, const N: usize>( &self, tracker: &mut LatencyTracker<D, I, H, N>, destinations: impl IntoIterator<Item = &'a D>, attempt: u32, now: I, ) -> Duration
where D: Hash + Eq + Clone + 'a, I: Instant, H: BuildHasher,

Computes an adaptive timeout for a request to the given destinations.

Returns the maximum timeout across all destinations, clamped to [min_timeout, max_timeout]. attempt is 1-based; higher attempts produce longer timeouts via exponential backoff.

Source

pub fn select_timeout_ms<'a, D, I, H, const N: usize>( &self, tracker: &mut LatencyTracker<D, I, H, N>, destinations: impl IntoIterator<Item = &'a D>, attempt: u32, now: I, ) -> u64
where D: Hash + Eq + Clone + 'a, I: Instant, H: BuildHasher,

Computes an adaptive timeout in milliseconds. See select_timeout.

Source

pub fn exponential_backoff(&self, attempt: u32) -> Duration

Pure exponential backoff: min_timeout * 2^(attempt - 1), clamped to max_timeout. Fallback when histogram data is insufficient.

Source

pub fn exponential_backoff_ms(&self, attempt: u32) -> u64

Pure exponential backoff in milliseconds.

Source

pub fn config(&self) -> &TimeoutConfig

Returns a reference to the timeout configuration.

Trait Implementations§

Source§

impl Clone for AdaptiveTimeout

Source§

fn clone(&self) -> AdaptiveTimeout

Returns a duplicate 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 Default for AdaptiveTimeout

Source§

fn default() -> AdaptiveTimeout

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

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> ToOwned for T
where T: Clone,

Source§

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