pub struct Interval { /* private fields */ }
Expand description
Interval controller.
Implementations§
Source§impl Interval
impl Interval
Sourcepub async fn tick_with_min_interval(
&mut self,
minimum_interval: Duration,
) -> Report
pub async fn tick_with_min_interval( &mut self, minimum_interval: Duration, ) -> Report
Wait until next interval.
This function will return [SleepResult
] which contains the duration that overly passed
the specified interval. As it internally aligns to the specified interval, it should not
be drifted over time, in terms of Instant
clock domain.
minimum_interval
: Minimum interval to wait. This prevents burst after long inactivity onInterval
object.
Sourcepub async fn tick(&mut self) -> Report
pub async fn tick(&mut self) -> Report
A shortcut for [tick_with_min_interval
] with minimum_interval
set to half.
Sourcepub fn set_interval(&mut self, interval: Duration)
pub fn set_interval(&mut self, interval: Duration)
Reset interval to the specified duration.
pub fn interval(&self) -> Duration
pub fn wakeup_time(&self) -> Instant
Sourcepub fn align_with_clock(
&mut self,
now_since_epoch: impl FnOnce() -> Duration,
interval: Option<Duration>,
initial_interval_tolerance: Option<Duration>,
align_offset_ns: i64,
)
pub fn align_with_clock( &mut self, now_since_epoch: impl FnOnce() -> Duration, interval: Option<Duration>, initial_interval_tolerance: Option<Duration>, align_offset_ns: i64, )
This method aligns the subsequent tick to a given interval. Following the alignment, the timestamp will conform to the specified interval.
Parameters:
now_since_epoch
: A function yielding the current time since the epoch. It’s internally converted to anInstant
, hence, should return the most recent timestamp.align_offset_ns
: This parameter can adjust the alignment timing. For example, an offset of 100us applied to a next tick scheduled at 9000us will push the tick to 9100us.initial_interval_tolerance
: This defines the permissible noise level for the initial interval. If set to zero, the actual sleep duration will exceed the interval, potentially causing a tick to be skipped as the actual sleep duration might be twice the interval. It’s advisable to set it to 10% of the interval to prevent thealign_clock
command from disrupting the initial interval.
Note: For example, if now_since_epoch()
gives 8500us and the interval is 1000us, the
subsequent tick will be adjusted to 9000us, aligning with the interval.
Sourcepub fn align_now(
&mut self,
interval: Option<Duration>,
initial_interval_tolerance: Option<Duration>,
align_offset_ns: i64,
)
pub fn align_now( &mut self, interval: Option<Duration>, initial_interval_tolerance: Option<Duration>, align_offset_ns: i64, )
Shortcut for [Self::align_clock
] from now.
Sourcepub fn align_with_system_clock(
&mut self,
interval: Option<Duration>,
initial_interval_tolerance: Option<Duration>,
align_offset_ns: i64,
)
pub fn align_with_system_clock( &mut self, interval: Option<Duration>, initial_interval_tolerance: Option<Duration>, align_offset_ns: i64, )
Shortcut for [Self::align_clock
] with std::time::SystemTime
as the time source.