clock

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package clock is the agent's source of time. Every component reads time through a Clock rather than calling time.Now directly, so runs are reproducible: tests and deterministic replay supply a Manual clock that only advances when told, while production uses System.

This is a deliberate foundational choice - deterministic replay and time-travel debugging are impossible if wall-clock time leaks in at arbitrary call sites.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Clock

type Clock interface {
	// Now returns the current time. Implementations should return UTC.
	Now() time.Time
}

Clock is the agent's source of the current time.

type Manual

type Manual struct {
	// contains filtered or unexported fields
}

Manual is a deterministic Clock for tests and replay: it returns a fixed time that only changes via Advance or Set. It is safe for concurrent use. Manual also implements Timing: timers it hands out fire only when Advance or Set moves the clock past their deadline, so time-based code (the reconciler workqueue) stays fully deterministic with no real sleeping.

func NewManual

func NewManual(start time.Time) *Manual

NewManual returns a Manual clock started at start (normalised to UTC).

func (*Manual) Advance

func (m *Manual) Advance(d time.Duration)

Advance moves the clock forward by d, firing any timers now due.

func (*Manual) After

func (m *Manual) After(d time.Duration) <-chan time.Time

After implements Timing.

func (*Manual) NewTimer

func (m *Manual) NewTimer(d time.Duration) Timer

NewTimer returns a timer that fires when the Manual clock is advanced past d from the current time. d <= 0 fires immediately.

func (*Manual) Now

func (m *Manual) Now() time.Time

Now implements Clock.

func (*Manual) PendingTimers

func (m *Manual) PendingTimers() int

PendingTimers returns how many timers are armed and not yet fired. It lets a test wait until asynchronously-scheduled work (a queue's backoff timer) has registered before advancing the clock, so time-driven behaviour is deterministic rather than racy.

func (*Manual) Set

func (m *Manual) Set(t time.Time)

Set moves the clock to t (normalised to UTC), firing any timers now due.

type System

type System struct{}

System is the production Clock, backed by the wall clock in UTC.

func (System) After

func (System) After(d time.Duration) <-chan time.Time

After returns a real wall-clock channel.

func (System) NewTimer

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

NewTimer returns a real wall-clock timer.

func (System) Now

func (System) Now() time.Time

Now implements Clock.

type Timer

type Timer interface {
	// C is the channel the timer fires on. It delivers at most one value.
	C() <-chan time.Time
	// Stop prevents a not-yet-fired timer from firing. It reports whether the
	// timer was still pending (true) or had already fired or been stopped (false),
	// matching time.Timer.Stop.
	Stop() bool
	// Reset restarts the timer to fire after d from now. It reports whether the
	// timer was still pending before the reset, matching time.Timer.Reset.
	Reset(d time.Duration) bool
}

Timer is a single-shot timer whose channel C fires once, when its deadline arrives. It mirrors the slice of time.Timer that delayed-work schedulers need, but is created through a Timing clock so a Manual clock makes timer-based code deterministic (the timer fires on Advance/Set, never on real wall time).

type Timing

type Timing interface {
	Clock
	// NewTimer returns a timer that fires once after d. A d <= 0 fires immediately.
	NewTimer(d time.Duration) Timer
	// After is shorthand for NewTimer(d).C().
	After(d time.Duration) <-chan time.Time
}

Timing is a Clock that can also schedule timers. Components that delay work (the reconciler workqueue's AddAfter/rate limiting) depend on Timing rather than Clock so tests drive every delay with a Manual clock instead of sleeping. Both System and Manual implement it.

Jump to

Keyboard shortcuts

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