Struct Timer

Source
pub(crate) struct Timer<R: SleepProvider> {
    sleep_prov: R,
    parameters: Option<PreparedParameters>,
    selected_timeout: Option<Duration>,
    trigger_at: Option<Instant>,
    waker: Option<R::SleepFuture>,
}
Expand description

Timer that organises wakeups when channel padding should be sent

Use next() to find when to send padding, and note_cell_sent() to reset the timeout when data flows.

A Timer can be in “disabled” state, in which case next() never completes.

Timer must be pinned before use (this allows us to avoid involving the allocator when we reschedule).

Fields§

§sleep_prov: R

[SleepProvider]

§parameters: Option<PreparedParameters>

Parameters controlling distribution of padding time intervals

Can be None to mean the timing parameters are set to infinity.

§selected_timeout: Option<Duration>

Gap that we intend to leave between last sent cell, and the padding

We only resample this (calculating a new random delay) after the previous timeout actually expired.

None if the timer is disabled. (This can be done explicitly, but also occurs on time calculation overflow.)

Invariants: this field may be Some or None regardless of the values of other fields. If this field is None then the values in trigger_at and waker are unspecified.

§trigger_at: Option<Instant>

Absolute time at which we should send padding

None if cells more recently sent than we were polled. That would mean that we are currently moving data out through this channel. The absolute timeout will need to be recalculated when the data flow pauses.

Some means our next has been demanded recently. Then trigger_at records the absolute timeout at which we should send padding, which was calculated the first time we were polled (after data).

Invariants: the value in this field is meaningful only if selected_timeout is Some.

If selected_timeout is Some, and trigger_at is therefore valid, it is (obviously) no later than selected_timeout from now.

See also waker.

§waker: Option<R::SleepFuture>

Actual waker from the SleepProvider

This is created and updated lazily, because we suspect that with some runtimes setting timeouts may be slow. Lazy updating means that with intermittent data traffic, we do not keep scheduling, descheduling, and adjusting, a wakeup time.

Invariants:

If selected_timeout is Some, the time at which this waker will trigger here is never later than trigger_at, and never later than selected_timeout from now.

The wakeup time here may well be earlier than trigger_at, and sooner than selected_timeout from now. It may even be in the past. When we wake up and discover this situation, we reschedule a new waker.

If selected_timeout is None, the value is unspecified. We may retain a Some in this case so that if SleepProvider is enhanced to support rescheduling, we can do that without making a new SleepFuture (and without completely reorganising this the Timer state structure.)

Implementations§

Source§

impl<R: SleepProvider> Timer<R>

Source

pub(crate) fn project<'pin>( self: Pin<&'pin mut Self>, ) -> PaddingTimerProj<'pin, R>

Source

pub(crate) fn project_ref<'pin>( self: Pin<&'pin Self>, ) -> __TimerProjectionRef<'pin, R>

Source§

impl<R: SleepProvider> Timer<R>

Source

pub(crate) fn new(sleep_prov: R, parameters: Parameters) -> Result<Self>

Create a new Timer

Source

pub(crate) fn new_disabled( sleep_prov: R, parameters: Option<Parameters>, ) -> Result<Self>

Create a new Timer which starts out disabled

Source

pub(crate) fn disable(self: &mut Pin<&mut Self>)

Disable this Timer

Idempotent.

Source

pub(crate) fn enable(self: &mut Pin<&mut Self>)

Enable this Timer

(If the timer was disabled, the timeout will only start to run when next() is next polled.)

Idempotent.

Source

pub(crate) fn reconfigure( self: &mut Pin<&mut Self>, parameters: &Parameters, ) -> Result<()>

Set this Timer’s parameters

Will not enable or disable the timer; that must be done separately if desired.

The effect may not be immediate: if we are already in a gap between cells, that existing gap may not be adjusted. (We don’t restart the timer since that would very likely result in a gap longer than either of the configured values.)

Idempotent.

Source

pub(crate) fn is_enabled(&self) -> bool

Enquire whether this Timer is currently enabled

Source

fn select_fresh_timeout(self: Pin<&mut Self>)

Select a fresh timeout (and enable, if possible)

Source

pub(crate) fn note_cell_sent(self: &mut Pin<&mut Self>)

Note that data has been sent (ie, reset the timeout, delaying the next padding)

Source

fn prepare_to_sleep( self: Pin<&mut Self>, now: Option<Instant>, ) -> SleepInstructions<'_, R>

Calculate when to send padding, and return a suitable waker

In the usual case returns SleepInstructions::Waker.

Source

pub(crate) fn next( self: Pin<&mut Self>, ) -> impl FusedFuture<Output = Padding> + '_

Wait until we should next send padding, and then return the padding message

Should be used as a low-priority branch within select_biased!.

(next() has to be selected on, along with other possible events, in the main loop, so that the padding timer runs concurrently with other processing; and it should be in a low-priority branch of select_biased! as an optimisation: that avoids calculating timeouts etc. until necessary, i.e. it calculates them only when the main loop would otherwise block.)

The returned future is async-cancel-safe, but once it yields, the padding must actually be sent.

Source

async fn next_inner(self: Pin<&mut Self>) -> Padding

Wait until we should next send padding (not FusedFuture)

Callers wants a [FusedFuture] because select! needs one.

Trait Implementations§

Source§

impl<'pin, R: SleepProvider> Unpin for Timer<R>
where PinnedFieldsOf<__Timer<'pin, R>>: Unpin,

Auto Trait Implementations§

§

impl<R> Freeze for Timer<R>
where R: Freeze, <R as SleepProvider>::SleepFuture: Freeze,

§

impl<R> RefUnwindSafe for Timer<R>
where R: RefUnwindSafe, <R as SleepProvider>::SleepFuture: RefUnwindSafe,

§

impl<R> Send for Timer<R>

§

impl<R> Sync for Timer<R>
where <R as SleepProvider>::SleepFuture: Sync,

§

impl<R> UnwindSafe for Timer<R>
where R: UnwindSafe, <R as SleepProvider>::SleepFuture: UnwindSafe,

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
§

impl<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

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
§

impl<T> Conv for T

§

fn conv<T>(self) -> T
where Self: Into<T>,

Converts self into T using Into<T>. Read more
§

impl<T> Downcast for T
where T: Any,

§

fn into_any(self: Box<T>) -> Box<dyn Any>

Converts Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.
§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Converts Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
§

fn as_any(&self) -> &(dyn Any + 'static)

Converts &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Converts &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
§

impl<T> DowncastSend for T
where T: Any + Send,

§

fn into_any_send(self: Box<T>) -> Box<dyn Any + Send>

Converts Box<Trait> (where Trait: DowncastSend) to Box<dyn Any + Send>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

§

fn into_any_sync(self: Box<T>) -> Box<dyn Any + Sync + Send>

Converts Box<Trait> (where Trait: DowncastSync) to Box<dyn Any + Send + Sync>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send>

Converts Arc<Trait> (where Trait: DowncastSync) to Arc<Any>, which can then be downcast into Arc<ConcreteType> where ConcreteType implements Trait.
§

impl<T> FmtForward for T

§

fn fmt_binary(self) -> FmtBinary<Self>
where Self: Binary,

Causes self to use its Binary implementation when Debug-formatted.
§

fn fmt_display(self) -> FmtDisplay<Self>
where Self: Display,

Causes self to use its Display implementation when Debug-formatted.
§

fn fmt_lower_exp(self) -> FmtLowerExp<Self>
where Self: LowerExp,

Causes self to use its LowerExp implementation when Debug-formatted.
§

fn fmt_lower_hex(self) -> FmtLowerHex<Self>
where Self: LowerHex,

Causes self to use its LowerHex implementation when Debug-formatted.
§

fn fmt_octal(self) -> FmtOctal<Self>
where Self: Octal,

Causes self to use its Octal implementation when Debug-formatted.
§

fn fmt_pointer(self) -> FmtPointer<Self>
where Self: Pointer,

Causes self to use its Pointer implementation when Debug-formatted.
§

fn fmt_upper_exp(self) -> FmtUpperExp<Self>
where Self: UpperExp,

Causes self to use its UpperExp implementation when Debug-formatted.
§

fn fmt_upper_hex(self) -> FmtUpperHex<Self>
where Self: UpperHex,

Causes self to use its UpperHex implementation when Debug-formatted.
§

fn fmt_list(self) -> FmtList<Self>
where &'a Self: for<'a> IntoIterator,

Formats each item in a sequence. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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 more
§

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

§

fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> R
where Self: Sized,

Pipes by value. This is generally the method you want to use. Read more
§

fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> R
where R: 'a,

Borrows self and passes that borrow into the pipe function. Read more
§

fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> R
where R: 'a,

Mutably borrows self and passes that borrow into the pipe function. Read more
§

fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
where Self: Borrow<B>, B: 'a + ?Sized, R: 'a,

Borrows self, then passes self.borrow() into the pipe function. Read more
§

fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
where Self: BorrowMut<B>, B: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.borrow_mut() into the pipe function. Read more
§

fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
where Self: AsRef<U>, U: 'a + ?Sized, R: 'a,

Borrows self, then passes self.as_ref() into the pipe function.
§

fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
where Self: AsMut<U>, U: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.as_mut() into the pipe function.
§

fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
where Self: Deref<Target = T>, T: 'a + ?Sized, R: 'a,

Borrows self, then passes self.deref() into the pipe function.
§

fn pipe_deref_mut<'a, T, R>( &'a mut self, func: impl FnOnce(&'a mut T) -> R, ) -> R
where Self: DerefMut<Target = T> + Deref, T: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.deref_mut() into the pipe function.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
§

impl<T> Tap for T

§

fn tap(self, func: impl FnOnce(&Self)) -> Self

Immutable access to a value. Read more
§

fn tap_mut(self, func: impl FnOnce(&mut Self)) -> Self

Mutable access to a value. Read more
§

fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
where Self: Borrow<B>, B: ?Sized,

Immutable access to the Borrow<B> of a value. Read more
§

fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
where Self: BorrowMut<B>, B: ?Sized,

Mutable access to the BorrowMut<B> of a value. Read more
§

fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
where Self: AsRef<R>, R: ?Sized,

Immutable access to the AsRef<R> view of a value. Read more
§

fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
where Self: AsMut<R>, R: ?Sized,

Mutable access to the AsMut<R> view of a value. Read more
§

fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
where Self: Deref<Target = T>, T: ?Sized,

Immutable access to the Deref::Target of a value. Read more
§

fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
where Self: DerefMut<Target = T> + Deref, T: ?Sized,

Mutable access to the Deref::Target of a value. Read more
§

fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self

Calls .tap() only in debug builds, and is erased in release builds.
§

fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self

Calls .tap_mut() only in debug builds, and is erased in release builds.
§

fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
where Self: Borrow<B>, B: ?Sized,

Calls .tap_borrow() only in debug builds, and is erased in release builds.
§

fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
where Self: BorrowMut<B>, B: ?Sized,

Calls .tap_borrow_mut() only in debug builds, and is erased in release builds.
§

fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
where Self: AsRef<R>, R: ?Sized,

Calls .tap_ref() only in debug builds, and is erased in release builds.
§

fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
where Self: AsMut<R>, R: ?Sized,

Calls .tap_ref_mut() only in debug builds, and is erased in release builds.
§

fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
where Self: Deref<Target = T>, T: ?Sized,

Calls .tap_deref() only in debug builds, and is erased in release builds.
§

fn tap_deref_mut_dbg<T>(self, func: impl FnOnce(&mut T)) -> Self
where Self: DerefMut<Target = T> + Deref, T: ?Sized,

Calls .tap_deref_mut() only in debug builds, and is erased in release builds.
§

impl<T> TryConv for T

§

fn try_conv<T>(self) -> Result<T, Self::Error>
where Self: TryInto<T>,

Attempts to convert self into T using TryInto<T>. 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.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

impl<T> ErasedDestructor for T
where T: 'static,

§

impl<T> MaybeSendSync for T