Skip to main content

ExponentialBackoff

Struct ExponentialBackoff 

Source
pub struct ExponentialBackoff<const SPIN_LIMIT: usize, const UNTIL_UNCHANGED_LIMIT: usize = 0, const YIELD_AFTER: usize = { usize::MAX }> { /* private fields */ }
Expand description

Performs exponential backoff.

Each backoff iteration iter (starting from 0) calls spin_loop 1 << iter.min(SPIN_LIMIT) times.

During the first UNTIL_UNCHANGED_LIMIT backoff iterations, backoff continues until the atomic’s value stops changing between reloads; after that, the CAS is retried after a single reload to avoid starvation.

After YIELD_AFTER backoff iterations (and if the std feature is enabled), yield_now is called instead of spinning.

For reference, crossbeam::utils::Backoff is equivalent to ExponentialBackoff<6> with Backoff::spin, and ExponentialBackoff<10, 0, 7> with Backoff::snooze. However, UNTIL_UNCHANGED_LIMIT should also be used in contended CAS loop to further reduce contention.

Implementations§

Source§

impl<const SPIN_LIMIT: usize, const UNTIL_UNCHANGED_LIMIT: usize, const YIELD_AFTER: usize> ExponentialBackoff<SPIN_LIMIT, UNTIL_UNCHANGED_LIMIT, YIELD_AFTER>

Source

pub fn starts_at(iter: usize) -> Self

Starts an exponential backoff at the given iteration (starting from 0).

This constructor can be used to skip the first smaller iterations.

Source

pub fn iter_count(&self) -> usize

Returns the current count of backoff iterations performed.

It can be used for example to switch to another algorithm, like thread parking, after a given iteration count.

Trait Implementations§

Source§

impl<const SPIN_LIMIT: usize, const UNTIL_UNCHANGED_LIMIT: usize, const YIELD_AFTER: usize> BackoffStrategy for ExponentialBackoff<SPIN_LIMIT, UNTIL_UNCHANGED_LIMIT, YIELD_AFTER>

Source§

fn backoff(&mut self) -> RetryStrategy

Performs backoff and returns how the CAS should be retried. Read more
Source§

fn will_reload(&self) -> bool

Returns true if the next call to backoff will not return RetryStrategy::NoReload. Read more
Source§

const BACKOFF: bool = true

Whether the strategy does backoff or not. Read more
Source§

fn backoff_reload<T: PartialEq, F: FnMut() -> T>( &mut self, current: T, reload: F, ) -> T

Performs backoff after a failed CAS and reloads the atomic value according to the returned RetryStrategy. Read more
Source§

fn backoff_until<C: BackoffUntilCondition, F: FnMut() -> C>( &mut self, f: F, ) -> C::Result

Loops until a condition is fulfilled, performing backoff at each iteration.
Source§

impl<const SPIN_LIMIT: usize, const UNTIL_UNCHANGED_LIMIT: usize, const YIELD_AFTER: usize> Debug for ExponentialBackoff<SPIN_LIMIT, UNTIL_UNCHANGED_LIMIT, YIELD_AFTER>

Source§

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

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

impl<const SPIN_LIMIT: usize, const UNTIL_UNCHANGED_LIMIT: usize, const YIELD_AFTER: usize> Default for ExponentialBackoff<SPIN_LIMIT, UNTIL_UNCHANGED_LIMIT, YIELD_AFTER>

Source§

fn default() -> ExponentialBackoff<SPIN_LIMIT, UNTIL_UNCHANGED_LIMIT, YIELD_AFTER>

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

Auto Trait Implementations§

§

impl<const SPIN_LIMIT: usize, const UNTIL_UNCHANGED_LIMIT: usize, const YIELD_AFTER: usize> Freeze for ExponentialBackoff<SPIN_LIMIT, UNTIL_UNCHANGED_LIMIT, YIELD_AFTER>

§

impl<const SPIN_LIMIT: usize, const UNTIL_UNCHANGED_LIMIT: usize, const YIELD_AFTER: usize> RefUnwindSafe for ExponentialBackoff<SPIN_LIMIT, UNTIL_UNCHANGED_LIMIT, YIELD_AFTER>

§

impl<const SPIN_LIMIT: usize, const UNTIL_UNCHANGED_LIMIT: usize, const YIELD_AFTER: usize> Send for ExponentialBackoff<SPIN_LIMIT, UNTIL_UNCHANGED_LIMIT, YIELD_AFTER>

§

impl<const SPIN_LIMIT: usize, const UNTIL_UNCHANGED_LIMIT: usize, const YIELD_AFTER: usize> Sync for ExponentialBackoff<SPIN_LIMIT, UNTIL_UNCHANGED_LIMIT, YIELD_AFTER>

§

impl<const SPIN_LIMIT: usize, const UNTIL_UNCHANGED_LIMIT: usize, const YIELD_AFTER: usize> Unpin for ExponentialBackoff<SPIN_LIMIT, UNTIL_UNCHANGED_LIMIT, YIELD_AFTER>

§

impl<const SPIN_LIMIT: usize, const UNTIL_UNCHANGED_LIMIT: usize, const YIELD_AFTER: usize> UnsafeUnpin for ExponentialBackoff<SPIN_LIMIT, UNTIL_UNCHANGED_LIMIT, YIELD_AFTER>

§

impl<const SPIN_LIMIT: usize, const UNTIL_UNCHANGED_LIMIT: usize, const YIELD_AFTER: usize> UnwindSafe for ExponentialBackoff<SPIN_LIMIT, UNTIL_UNCHANGED_LIMIT, YIELD_AFTER>

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> 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 = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.