Your browser sucks, JavaScript disabled! Gunnar's console needs JavaScript and WebAssembly. Git over SSH on port 2222 does not.

nordisk/monetize

crates/monetize-cloud/src/pace.rs

//! **The clock and the wait a provider polls with.**
//!
//! A provider that buys waits: a disk is `maintenance` for seconds after its
//! `POST`, a server `started` a minute after its create. Linked into the
//! server it reads the process clock and sleeps its own thread. Running as a
//! guest it has neither — no clock, no thread, and `std::time::Instant`
//! panics on `wasm32-unknown-unknown` — so it asks the host for both. The
//! loop is the same code either way; only its [`Pace`] differs.

use std::time::Duration;

/// Where a provider reads the time and waits.
pub trait Pace: Send + Sync {
    /// Unix milliseconds.
    fn now_ms(&self) -> u64;
    /// Wait `d`.
    fn sleep(&self, d: Duration);
}

/// The process's own clock and thread — a linked provider's.
#[derive(Clone, Copy, Debug, Default)]
pub struct SystemPace;

impl Pace for SystemPace {
    fn now_ms(&self) -> u64 {
        std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map(|d| d.as_millis() as u64).unwrap_or_default()
    }
    fn sleep(&self, d: Duration) {
        std::thread::sleep(d)
    }
}