tickloop

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package tickloop provides a fixed-timestep simulation scheduler with optional 2× sub-tick lanes. Subsystems register against either lane and are invoked in registration order each tick. The scheduler tracks overrun (caps catch-up steps), invokes a Recorder for metrics, and shuts down cleanly on context cancellation.

Allocation-free in steady state after warmup.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Rate is sim ticks per second (e.g. 30).
	Rate int
	// SubRate is sub-ticks per second; must be a positive multiple of Rate
	// or 0 to disable.
	SubRate int
	// MaxCatchup bounds catch-up step count when behind schedule.
	MaxCatchup int
	// Recorder receives per-tick metrics; nil → NoopRecorder.
	Recorder Recorder
	// Clock; nil → wall clock.
	Clock func() time.Time
}

Config controls scheduler rates and behaviour.

type Frame

type Frame struct {
	Tick uint64
	Now  time.Time
	Dt   time.Duration
}

Frame is the per-tick context handed to Subsystems.

type NoopRecorder

type NoopRecorder struct{}

NoopRecorder satisfies Recorder without observing anything.

func (NoopRecorder) OnOverrun

func (NoopRecorder) OnOverrun(int)

func (NoopRecorder) OnSubtick

func (NoopRecorder) OnSubtick(SubtickFrame, time.Duration)

func (NoopRecorder) OnTick

func (NoopRecorder) OnTick(Frame, time.Duration)

type Recorder

type Recorder interface {
	OnTick(frame Frame, took time.Duration)
	OnSubtick(frame SubtickFrame, took time.Duration)
	OnOverrun(missedTicks int)
}

Recorder receives metrics callbacks. Implementations should be cheap; they're invoked every tick.

type Scheduler

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

Scheduler ticks Subsystems on a fixed schedule.

func New

func New(cfg Config, subs ...Subsystem) *Scheduler

New constructs a Scheduler. Panics on invalid config.

func (*Scheduler) Run

func (s *Scheduler) Run(ctx context.Context) error

Run drives the tick loop until ctx is done. Returns ctx.Err() on exit.

type Subsystem

type Subsystem interface {
	Label() string
	Step(frame Frame)
}

Subsystem runs on the main simulation tick.

type SubtickFrame

type SubtickFrame struct {
	Frame
	SubIndex uint8
}

SubtickFrame extends Frame with a sub-index for double-rate lanes.

type SubtickSubsystem

type SubtickSubsystem interface {
	Subsystem
	SubStep(frame SubtickFrame)
}

SubtickSubsystem runs on the higher-rate sub-tick lane.

Jump to

Keyboard shortcuts

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