clock

package
v0.24.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 22, 2026 License: Apache-2.0 Imports: 4 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package clock is the module's one time port: the current time, one-shot timers and tickers behind an interface, so that a test can drive every interval of the stack — MRP retransmission, the acknowledgement pump, the subscription reporting loop, commissioning windows, cluster timers — by hand instead of waiting for the wall clock.

Every package of the module takes its time from here: nothing outside this package calls time.Now, time.AfterFunc, time.NewTimer, time.NewTicker, time.Sleep, time.After, time.Tick, time.Since or time.Until in production code (clock/noraw_test.go fails the build on one). System is the standard library and the default of every owner; github.com/SukramJ/go-fabric/clock/clocktest.Fake is the test clock.

A host injects a clock through the owner's configuration — the `Clock clock.Clock` field of github.com/SukramJ/go-fabric/node.Config, github.com/SukramJ/go-fabric/bridge.Config and the other owners. A nil clock is System. A node hands its clock on to the bridge, the store's persist loop, OTA and mDNS, so one field moves the whole device side.

Clock.AfterFunc runs f in its own goroutine, as time.AfterFunc does; f must not assume it runs on the goroutine that armed the timer.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func After

func After(c Clock, d time.Duration) <-chan time.Time

After is c.NewTimer(d).C(), time.After on c. The timer is not stopped when the caller stops waiting; use Clock.NewTimer where that matters.

func MonotonicOf

func MonotonicOf(c Clock) time.Duration

MonotonicOf is c's Monotonic reading, or — for a clock without one — the wall reading since the Unix epoch, which then follows every step.

func Since

func Since(c Clock, t time.Time) time.Duration

Since is c.Now().Sub(t), time.Since on c.

func Sleep

func Sleep(c Clock, d time.Duration, done <-chan struct{}) bool

Sleep blocks on c for d or until done is closed, whichever comes first, and reports whether the full d elapsed. A nil done never closes.

func Until

func Until(c Clock, t time.Time) time.Duration

Until is t.Sub(c.Now()), time.Until on c.

func WallOf

func WallOf(c Clock, mono time.Duration) time.Time

WallOf converts a Monotonic reading taken from c into the wall-clock time c reads now for it — matter.js Session.ts:154-162 (wallClockActivity: `timestamp + (Time.nowMs - Time.nowUs)`). Whether a reading was taken at all is the caller's to know.

func WithDeadline added in v0.11.0

func WithDeadline(parent context.Context, c Clock, at time.Time) (context.Context, context.CancelFunc)

WithDeadline is context.WithDeadline measured on c: WithTimeout for the time from c's now until at. A nil c and System are context.WithDeadline itself.

func WithTimeout added in v0.11.0

func WithTimeout(parent context.Context, c Clock, d time.Duration) (context.Context, context.CancelFunc)

WithTimeout is context.WithTimeout measured on c: the returned context ends with context.DeadlineExceeded once d has passed on c, or when parent ends, or when cancel is called, whichever comes first. A nil c and System are context.WithTimeout itself, so a host on the wall clock sees no change.

On any other clock the deadline is a timer of c: a github.com/SukramJ/go-fabric/clock/clocktest.Fake ends the context when it is advanced past d, never on the machine's time, so a deadline in code a test paces on a fake clock cannot expire while the test's clock stands. Such a context reports no Deadline of its own (a stdlib context derived from it would measure an instant of c on the wall clock); its Err is context.DeadlineExceeded once the timer fired, as context.Cause of it and of every context derived from it is.

This is the deadline form of matter.js's Time port: every timer and timeout of the stack goes through the injectable Time (packages/general/src/time/Time.ts:73-118 at b25ed960, getTimer and sleep), so MockTime decides when it fires.

Types

type Clock

type Clock interface {
	// Now is the current time.
	Now() time.Time
	// AfterFunc calls f in its own goroutine once d has elapsed. The
	// returned Timer's C is nil.
	AfterFunc(d time.Duration, f func()) Timer
	// NewTimer sends the current time on the Timer's C once d has elapsed.
	NewTimer(d time.Duration) Timer
	// NewTicker sends the current time on the Ticker's C every d. d must
	// be greater than zero.
	NewTicker(d time.Duration) Ticker
}

Clock is a time source: the current time, one-shot timers and tickers. Its methods are safe for concurrent use.

func Or

func Or(c Clock) Clock

Or is c, or System when c is nil — the default every owner applies.

type MatterTime

type MatterTime func() (epochS uint32, synced bool)

MatterTime is a node's Matter time: the current Matter epoch second (Matter §A.2, epoch-s) and whether the node has a time at all — false while its UTCTime is null. A node's own is its root TimeSynchronization's UTC (cluster/core.TimeSynchronization.MatterEpochSeconds), which it hands the servers that read Matter time (the Thermostat's suggestions, the Valve's AutoCloseTime) as they join its topology.

func MatterTimeOf

func MatterTimeOf(c Clock) MatterTime

MatterTimeOf is the Matter time of c (nil: System): its current time in Matter epoch seconds, false before the epoch or past what epoch-s holds. It is the time of a server outside a node whose host clock is its time source — the host attaches it (thermo.ThermostatServer.AttachMatterTime, valve.Server.AttachMatterTime); a node hands its own, which follows the UTC its TimeSynchronization adopted.

type Monotonic

type Monotonic interface {
	// Monotonic is the elapsed reading. Only differences of two readings
	// of the same clock mean anything.
	Monotonic() time.Duration
}

Monotonic is the optional second reading of a Clock: elapsed time that only advances, unaffected by a wall-clock step (an NTP correction, a manual clock set). Code that measures an interval — a session's activity threshold, a peer-loss cutoff, the age of a cache entry — reads it through MonotonicOf and converts to the wall clock only for exposure, the way matter.js keeps its timestamps on Time.nowUs (the monotonic reading, packages/general/src/time/Time.ts:53-64 at db021da0) and reports them through the current wall-clock offset (packages/protocol/src/session/Session.ts:155-163).

System implements it with the monotonic reading time.Now carries; github.com/SukramJ/go-fabric/clock/clocktest.Fake implements it with its advanced time, which [Fake.StepWall] leaves alone.

type System

type System struct{}

System is the Clock of the time package. Its zero value is ready.

func (System) AfterFunc

func (System) AfterFunc(d time.Duration, f func()) Timer

AfterFunc is time.AfterFunc.

func (System) Monotonic

func (System) Monotonic() time.Duration

Monotonic implements Monotonic on the monotonic reading time.Now carries (time.Time.Sub uses it when both operands have one).

func (System) NewTicker

func (System) NewTicker(d time.Duration) Ticker

NewTicker is time.NewTicker.

func (System) NewTimer

func (System) NewTimer(d time.Duration) Timer

NewTimer is time.NewTimer.

func (System) Now

func (System) Now() time.Time

Now is time.Now.

type Ticker

type Ticker interface {
	// Stop turns the ticker off; no more ticks are sent.
	Stop()
	// Reset stops the ticker and restarts it with period d.
	Reset(d time.Duration)
	// C is the channel the ticks are delivered on.
	C() <-chan time.Time
}

Ticker is a periodic ticker a Clock started, with time.Ticker's semantics: a slow receiver misses ticks rather than queueing them.

type Timer

type Timer interface {
	// Stop prevents the timer from firing. It reports whether it did:
	// false means the timer had already fired or been stopped.
	Stop() bool
	// Reset re-arms the timer to fire after d. It reports whether the
	// timer had been active.
	Reset(d time.Duration) bool
	// C is the channel a timer from [Clock.NewTimer] delivers on; nil for
	// a timer from [Clock.AfterFunc].
	C() <-chan time.Time
}

Timer is a one-shot timer a Clock started. Its semantics are those of time.Timer since Go 1.23: after Stop or Reset returns, no stale value is received from C.

Directories

Path Synopsis
Package clocktest is the test clock of the module: Fake, a clock.Clock whose time moves only when the test moves it.
Package clocktest is the test clock of the module: Fake, a clock.Clock whose time moves only when the test moves it.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL