Skip to main content

TimeSource

Trait TimeSource 

Source
pub trait TimeSource:
    Send
    + Sync
    + Clone
    + 'static {
    // Required methods
    fn now_nanos(&self) -> u64;
    fn sleep(
        &self,
        duration: Duration,
    ) -> Pin<Box<dyn Future<Output = ()> + Send>>;
    fn sleep_until(
        &self,
        deadline_nanos: u64,
    ) -> Pin<Box<dyn Future<Output = ()> + Send>>;
    fn timeout<F, T>(
        &self,
        duration: Duration,
        future: F,
    ) -> Pin<Box<dyn Future<Output = Option<T>> + Send>>
       where F: Future<Output = T> + Send + 'static,
             T: Send + 'static;

    // Provided methods
    fn now(&self) -> Duration { ... }
    fn connection_idle_timeout(&self) -> Duration { ... }
    fn supports_keepalive(&self) -> bool { ... }
}
Expand description

Abstraction over time operations supporting both real and virtual time.

This trait allows code to be written once and run in both production (with real time) and simulation (with virtual time) contexts.

Required Methods§

Source

fn now_nanos(&self) -> u64

Returns the current time as nanoseconds since an arbitrary epoch.

Source

fn sleep(&self, duration: Duration) -> Pin<Box<dyn Future<Output = ()> + Send>>

Creates a future that completes after the given duration.

Source

fn sleep_until( &self, deadline_nanos: u64, ) -> Pin<Box<dyn Future<Output = ()> + Send>>

Creates a future that completes when the deadline is reached or returns immediately if the deadline has passed.

Source

fn timeout<F, T>( &self, duration: Duration, future: F, ) -> Pin<Box<dyn Future<Output = Option<T>> + Send>>
where F: Future<Output = T> + Send + 'static, T: Send + 'static,

Wraps a future with a timeout, returning None if the timeout expires.

Provided Methods§

Source

fn now(&self) -> Duration

Returns the current time as a Duration since an arbitrary epoch.

Source

fn connection_idle_timeout(&self) -> Duration

Returns the duration after which an idle connection should be considered dead.

VirtualTime uses a very long timeout (1 hour) to avoid premature disconnections when time is advanced rapidly by auto-advance tasks. RealTime uses the standard 120 seconds.

Source

fn supports_keepalive(&self) -> bool

Returns whether keepalive pings should be enabled for connections using this time source.

VirtualTime returns false because:

  1. The interaction between auto-advance and keepalive timing causes spurious failures
  2. Aborting the keepalive task with VirtualTime causes hangs (abort() + VirtualSleep issue)
  3. Keepalive is meaningless for simulated connections anyway

See issue #2695 for details.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§