idle

package
v1.26.0 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package idle implements activity-driven worker suspension: it tracks when each site was last active and (later) suspends a quiet site's suspendable workers, resuming them on the next activity. This file is the activity tracker — the pure, in-memory record of "site X was last active at T" fed by the nginx access feed, file-edit events, and CLI/MCP actions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LoadActivity

func LoadActivity(path string) map[string]int64

LoadActivity reads a last-active map previously written by Save, returning the site->unix-seconds map (nil when the file is missing or unreadable).

func ParseAccessHost

func ParseAccessHost(datagram []byte) string

ParseAccessHost extracts the request host from one nginx access datagram. The access feed logs a minimal "$host" message through syslog framing, so the host is the final whitespace-delimited token of the line. Returns "" for nginx's "-" placeholder (a request with no Host header) or an unparseable line.

Types

type Action

type Action int

Action is what the engine should do for a site at one evaluation.

const (
	ActionNone Action = iota
	ActionSuspend
	ActionResume
)

func Decide

func Decide(enabled bool, timeout, idleFor time.Duration, hasRecord, suspended bool) Action

Decide returns the action for a site given its resolved idle-suspend policy and current state. It is pure so the suspend/resume rules can be exhaustively tested without touching workers or the clock.

disabled + suspended   -> resume   (feature turned off; restore what we stopped)
disabled               -> none
no activity record yet  -> none     (startup grace; never suspend a site we've
                                      not yet observed)
idle >= timeout, up      -> suspend
idle <  timeout, down    -> resume   (activity returned; backstops OnActivity)
otherwise                -> none

func (Action) String

func (a Action) String() string

type SiteResolver

type SiteResolver func(host string) (site string, ok bool)

SiteResolver maps a request host (e.g. "admin.myapp.test") to the owning site name, collapsing a site's many domains onto one activity record. It returns ok=false when the host belongs to no registered site.

type Tracker

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

Tracker records the last-active time per site. It is safe for concurrent use: the access-feed listener, the watcher bridge, and the engine ticker all touch it from different goroutines.

func NewTracker

func NewTracker(resolve SiteResolver) *Tracker

NewTracker returns a tracker that resolves request hosts to sites via resolve. resolve may be nil, in which case TouchHost is a no-op (only TouchSite works).

func (*Tracker) Forget

func (t *Tracker) Forget(site string)

Forget drops a site's record, e.g. when a site is removed. No-op if absent.

func (*Tracker) IdleFor

func (t *Tracker) IdleFor(site string, now time.Time) (time.Duration, bool)

IdleFor reports how long the site has been idle as of now. A site with no recorded activity reports ok=false rather than a misleading "infinitely idle", so callers can apply a startup grace window instead of suspending immediately.

func (*Tracker) LastActive

func (t *Tracker) LastActive(site string) (time.Time, bool)

LastActive returns the last-active time for a site and whether one is recorded.

func (*Tracker) Save

func (t *Tracker) Save(path string) error

Save persists the last-active map to path as JSON ({site: unixSeconds}) so a restart can restore the idle countdowns instead of re-seeding to now. Written atomically via a temp file + rename.

func (*Tracker) Seed

func (t *Tracker) Seed(sites []string, at time.Time)

Seed marks the given sites active as of t, used on lerd-ui startup so a restart's empty map doesn't make every site look instantly idle and get suspended inside its grace window. Seeding never overwrites a newer record.

func (*Tracker) Snapshot

func (t *Tracker) Snapshot() map[string]time.Time

Snapshot returns a copy of the last-active map for read-only consumers (the dashboard, diagnostics) without exposing the internal map.

func (*Tracker) TouchHost

func (t *Tracker) TouchHost(host string, at time.Time) string

TouchHost resolves a request host to its site and records activity. Hosts that belong to no site are ignored. Returns the resolved site (or "" when unmatched).

func (*Tracker) TouchSite

func (t *Tracker) TouchSite(site string, at time.Time)

TouchSite records activity for a known site name at time t. Older timestamps never move the record backwards, so out-of-order datagrams are harmless.

Jump to

Keyboard shortcuts

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