governor

package
v0.9.3 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2026 License: Apache-2.0 Imports: 14 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 governor is the resource-governance policy engine (SPEC §4.1, §10). It consumes idle/power signals, enforces the operator's serve policy, and guarantees instant-yield: on operator activity, in-flight requests are drained-or-cancelled within the configured grace window and the node marks itself YIELDED.

Index

Constants

View Source
const (
	AwakeReasonRequest = "Teraflock: inference request in flight"
	AwakeReasonServing = "Teraflock: serving the mesh"
)

Reasons the governor holds the inhibitor for, as the OS shows them.

Variables

View Source
var ErrNoIdleSource = errors.New("governor: idle detection not implemented on this platform")

ErrNoIdleSource is returned by stub platform sources.

View Source
var ErrNoSleepInhibitor = errors.New("governor: sleep inhibitor not implemented on this platform")

ErrNoSleepInhibitor is what the stub platform inhibitor returns from Hold: the governor logs it once and carries on without an assertion.

Functions

This section is empty.

Types

type ErrNotServing

type ErrNotServing struct{ State State }

ErrNotServing is returned by Admit when the governor refuses work.

func (ErrNotServing) Error

func (e ErrNotServing) Error() string

type FakeIdleSource

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

FakeIdleSource / FakePowerSource are shared by tests here and in other packages (localapi, tunnel) that need a controllable governor.

func (*FakeIdleSource) Fail

func (f *FakeIdleSource) Fail(err error)

func (*FakeIdleSource) IdleFor

func (*FakeIdleSource) Set

func (f *FakeIdleSource) Set(d time.Duration)

type FakePowerSource

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

func (*FakePowerSource) Set

func (f *FakePowerSource) Set(ps PowerStatus)

func (*FakePowerSource) Status

type FakeSleepInhibitor added in v0.9.0

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

FakeSleepInhibitor records holds and releases for tests (here and in packages that build a governor with one).

func (*FakeSleepInhibitor) Counts added in v0.9.0

func (f *FakeSleepInhibitor) Counts() (holds []string, releases int)

Counts reports every reason Hold was called with, in order, and how many times Release was.

func (*FakeSleepInhibitor) Fail added in v0.9.0

func (f *FakeSleepInhibitor) Fail(err error)

Fail makes the next Holds return err (nil clears it).

func (*FakeSleepInhibitor) Held added in v0.9.0

func (f *FakeSleepInhibitor) Held() (bool, string)

Held reports whether the fake is held and with which reason.

func (*FakeSleepInhibitor) Hold added in v0.9.0

func (f *FakeSleepInhibitor) Hold(reason string) error

func (*FakeSleepInhibitor) Release added in v0.9.0

func (f *FakeSleepInhibitor) Release() error

type Governor

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

Governor evaluates Policy against live signals.

func New

func New(p Policy, idle IdleSource, power PowerSource, clk clock.Clock, log *slog.Logger) *Governor

New builds a Governor. clk may be nil (wall clock).

func (*Governor) Admit

func (g *Governor) Admit(parent context.Context, id string) (context.Context, func(), error)

Admit gates one request. On success it returns a request context that the governor cancels if the yield grace expires, plus a release func the caller MUST invoke when the request finishes (success or not).

func (*Governor) AwakeReason added in v0.9.0

func (g *Governor) AwakeReason() string

AwakeReason reports why the sleep assertion is held ("" when it is not), for status output and tests.

func (*Governor) Close added in v0.9.0

func (g *Governor) Close()

Close releases the sleep assertion for good: later syncs are no-ops. Called on daemon exit; stop/exit must never leave the machine insomniac.

func (*Governor) Inflight

func (g *Governor) Inflight() int

Inflight returns the number of admitted, unfinished requests.

func (*Governor) Policy

func (g *Governor) Policy() Policy

Policy returns a copy of the active policy.

func (*Governor) Power

func (g *Governor) Power() PowerStatus

Power returns the last observed power status (for heartbeats).

func (*Governor) Run

func (g *Governor) Run(ctx context.Context)

Run polls signals until ctx ends.

func (*Governor) SetEnrolled added in v0.9.0

func (g *Governor) SetEnrolled(enrolled bool)

SetEnrolled tells the governor whether the node is part of a mesh. An enrolled node that is serving stays awake so placements can reach it ("serve the mesh while you sleep" means the operator sleeps, not the machine); an unenrolled or standalone daemon only stays awake for the request it is answering right now.

func (*Governor) SetPolicy

func (g *Governor) SetPolicy(p Policy)

SetPolicy swaps the policy at runtime (`tera limits` / PUT /api/v1/limits). The next evaluate() applies it.

func (*Governor) SetSleepInhibitor added in v0.9.0

func (g *Governor) SetSleepInhibitor(i SleepInhibitor)

SetSleepInhibitor installs the platform inhibitor (nil = none: the OS sleeps whenever it likes, which is the pre-v0.7 behaviour). Call it before Run; the first sync happens on the next transition.

func (*Governor) State

func (g *Governor) State() State

State returns the current decision.

func (*Governor) Subscribe

func (g *Governor) Subscribe() <-chan State

Subscribe returns a channel receiving state transitions (best-effort; slow consumers may miss intermediate states but always converge).

type IdleSource

type IdleSource interface {
	IdleFor(ctx context.Context) (time.Duration, error)
}

IdleSource reports how long user input has been quiet. Platform implementations live in idle_*.go; tests use FakeIdleSource.

func NewPlatformIdleSource

func NewPlatformIdleSource() IdleSource

NewPlatformIdleSource returns the Linux idle source: systemd-logind's IdleHint / IdleSinceHint / LockedHint for the operator's seat session, read through `loginctl show-session`. The session is $XDG_SESSION_ID when the daemon runs inside a login session, else the first active seat session from `loginctl list-sessions` (a systemd --user service has no XDG_SESSION_ID). No logind, or no seat session at all (headless), is ErrNoIdleSource: the governor assumes idle with a one-time log line, which is the right default for servers.

Caveat: IdleHint is set by the desktop session (GNOME, KDE, sway with an idle daemon…); a bare X session without one leaves it at "no", so the node only counts as idle there while the screen is locked (LockedHint, set by any locker that tells logind — loginctl lock-session, GNOME, KDE, swaylock via swayidle…). Without either, serve_policy = always or scheduled is the operator's answer.

type Policy

type Policy struct {
	Serve          string // always|idle-only|scheduled
	IdleAfter      time.Duration
	YieldGrace     time.Duration // drain-or-cancel window, default 2s
	PollInterval   time.Duration
	ServeOnBattery bool
	MaxTempCelsius float64
	Schedule       []Window
	// KeepAwake lets the governor hold a system-sleep assertion while a
	// request is in flight or while serving and enrolled (docs/sleep.md).
	// False = flockd never touches power management.
	KeepAwake bool
}

Policy is the operator's resource-governance configuration.

type PowerSource

type PowerSource interface {
	Status(ctx context.Context) (PowerStatus, error)
}

PowerSource reports battery/thermal state.

func NewPlatformPowerSource

func NewPlatformPowerSource() PowerSource

NewPlatformPowerSource reads /sys/class/power_supply and thermal zones.

type PowerStatus

type PowerStatus struct {
	OnBattery   bool
	TempCelsius float64
}

PowerStatus is a battery/thermal snapshot. TempCelsius == 0 means unknown.

type SleepInhibitor added in v0.9.0

type SleepInhibitor interface {
	// Hold takes the assertion, tagged with a reason the operator can read
	// in the OS's own tooling (powercfg /requests, pmset -g assertions,
	// systemd-inhibit --list). Holding an already-held inhibitor is a
	// no-op; the first reason stays.
	Hold(reason string) error
	// Release drops the assertion. Releasing an unheld inhibitor is a
	// no-op.
	Release() error
}

SleepInhibitor keeps the operating system from going to sleep on its own while held. It is the one place flockd touches power management, and it is deliberately narrow: an inhibitor stops the *automatic idle* sleep timer only. It never keeps the display on, never blocks a sleep the operator asks for (lid, menu, power button), and never wakes the machine. Platform implementations live in power_*.go; the Governor decides when to hold it (see Governor.syncAwake) and tests use FakeSleepInhibitor.

func NewPlatformSleepInhibitor added in v0.9.0

func NewPlatformSleepInhibitor(log *slog.Logger) SleepInhibitor

NewPlatformSleepInhibitor returns the Linux inhibitor: a logind `--what=idle` inhibitor lock, taken through `systemd-inhibit` the way the idle source reads logind through `loginctl` (no D-Bus client library). `idle` is the one logind inhibitor class that matches the policy: it stops the *automatic* idle action (suspend after N minutes of no input) and nothing else — `sleep` would also block or delay a suspend the operator asks for, which the policy forbids. The lock is held while the child runs; the child is `tail --pid=<flockd pid>`, so it ends on its own if flockd dies without releasing. `systemd-inhibit --list` shows it as flockd with the reason. Desktops that consult only their own session inhibitors (not logind's) may sleep anyway; see docs/sleep.md.

type State

type State int

State is the governor's serving decision.

const (
	// StateServing: the node accepts and runs inference.
	StateServing State = iota
	// StateYielded: the operator is using the machine (idle-only policy).
	StateYielded
	// StatePausedBattery: on battery and serve_on_battery=false.
	StatePausedBattery
	// StatePausedThermal: above the temperature ceiling.
	StatePausedThermal
	// StateOutsideSchedule: serve_policy=scheduled and now is out of window.
	StateOutsideSchedule
)

func (State) NodeState

func (s State) NodeState() typesv1.NodeState

NodeState maps to the tunnel heartbeat enum.

func (State) String

func (s State) String() string

type Window

type Window struct {
	StartMin int // minutes since midnight
	EndMin   int
}

Window is a daily serving window in local time. Overnight windows (start > end, e.g. 22:00-08:00) wrap across midnight.

func ParseWindow

func ParseWindow(s string) (Window, error)

ParseWindow parses "HH:MM-HH:MM".

func ParseWindows

func ParseWindows(ss []string) ([]Window, error)

ParseWindows parses a config list like ["22:00-08:00", "12:00-13:00"].

func (Window) Contains

func (w Window) Contains(t time.Time) bool

Contains reports whether t (local time-of-day) is inside the window.

Jump to

Keyboard shortcuts

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