pub struct ElectionTimer { /* private fields */ }Expand description
A Raft election timer with a configurable randomised timeout range.
The timer uses a lightweight LCG (linear congruential generator) for
deterministic tests via ElectionTimer::new_with_seed.
Implementations§
Source§impl ElectionTimer
impl ElectionTimer
Sourcepub fn new(min_timeout_ms: u64, max_timeout_ms: u64) -> Self
pub fn new(min_timeout_ms: u64, max_timeout_ms: u64) -> Self
Create a new timer with a random seed based on constant folding.
min_timeout_ms must be < max_timeout_ms.
Sourcepub fn new_with_seed(
min_timeout_ms: u64,
max_timeout_ms: u64,
seed: u64,
) -> Self
pub fn new_with_seed( min_timeout_ms: u64, max_timeout_ms: u64, seed: u64, ) -> Self
Create a new timer with an explicit seed for deterministic behaviour.
Sourcepub fn reset(&mut self, now_ms: u64)
pub fn reset(&mut self, now_ms: u64)
Reset the timer using a freshly drawn random timeout, starting the
deadline from now_ms.
After a call to reset, the state transitions to Running.
Sourcepub fn check(&mut self, now_ms: u64) -> bool
pub fn check(&mut self, now_ms: u64) -> bool
Check whether the timer has expired given the current time now_ms.
- If
Idle→ returnsfalse(idle timers don’t expire). - If
Runningandnow_ms >= deadline_ms→ transitions toExpiredand returnstrue. - If
Runningand not yet past deadline → returnsfalse. - If already
Expired→ returnstrue.
Sourcepub fn state(&self) -> &TimerState
pub fn state(&self) -> &TimerState
Return a reference to the current timer state.
Sourcepub fn current_timeout_ms(&self) -> u64
pub fn current_timeout_ms(&self) -> u64
Return the timeout duration that was set on the last call to
reset, or 0 if the timer has never been reset.
Sourcepub fn timeout_range(&self) -> (u64, u64)
pub fn timeout_range(&self) -> (u64, u64)
Return (min_timeout_ms, max_timeout_ms).
Auto Trait Implementations§
impl Freeze for ElectionTimer
impl RefUnwindSafe for ElectionTimer
impl Send for ElectionTimer
impl Sync for ElectionTimer
impl Unpin for ElectionTimer
impl UnsafeUnpin for ElectionTimer
impl UnwindSafe for ElectionTimer
Blanket Implementations§
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
Source§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreimpl<T> MaybeDebug for T
Source§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.