governor

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2026 License: Apache-2.0 Imports: 13 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

This section is empty.

Variables

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

ErrNoIdleSource is returned by stub platform sources.

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 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) 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) 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) 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
}

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 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