moonzero · control plane v0.15.0 · 49 packages · 4 targets · Apache-2.0 docs / control / clock GitHub

08 / CONTROL

clock

A millisecond time source injected into the resilience middlewares so their timing is a pure function of an explicit clock; ManualClock drives them deterministically in tests.

struct Clock

structA monotonic time source in **milliseconds**, injected into the resilience middlewares (rate-limit, breaker, timeout) so their timing logic is a pure function of an explicit clock rather than a hidden wall-clock read. reads timex.Now() directly; because that is neither portable across MoonBit's backends nor testable, moonzero threads the clock as a value — the same pattern Go's clockwork/x/time/rate accept for a Clock.

fn Clock::new(now_ms : () -> Int64) -> Clock

fnWrap a now-in-milliseconds thunk as a Clock.

fn Clock::now(self : Clock) -> Int64

fnThe current time in milliseconds, as reported by the wrapped source.

fn Clock::system() -> Clock

fnThe platform clock, in milliseconds since the Unix epoch`). Every backend can read it, so a service that has no reason to inject its own time source can take this one.

struct ManualClock

structA deterministic, hand-advanced clock for tests and for driving the rate-limit / breaker cores without a real time source. Wall time is replaced by an explicit advance, so a token bucket's refill or a breaker's open window can be exercised exactly.

fn ManualClock::new(start~ : Int64 = 0) -> ManualClock

fnA manual clock starting at start milliseconds (default 0).

fn ManualClock::advance(self : ManualClock, delta : Int64) -> Unit

fnMove the manual clock forward by delta milliseconds.

fn ManualClock::as_clock(self : ManualClock) -> Clock

fnA Clock view over this manual clock: reading it reflects every advance.