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 ¶
- func LoadActivity(path string) map[string]int64
- func ParseAccessHost(datagram []byte) string
- type Action
- type SiteResolver
- type Tracker
- func (t *Tracker) Forget(site string)
- func (t *Tracker) IdleFor(site string, now time.Time) (time.Duration, bool)
- func (t *Tracker) LastActive(site string) (time.Time, bool)
- func (t *Tracker) Save(path string) error
- func (t *Tracker) Seed(sites []string, at time.Time)
- func (t *Tracker) Snapshot() map[string]time.Time
- func (t *Tracker) TouchHost(host string, at time.Time) string
- func (t *Tracker) TouchSite(site string, at time.Time)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func LoadActivity ¶
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 ¶
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.
func Decide ¶
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
type SiteResolver ¶
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 ¶
Forget drops a site's record, e.g. when a site is removed. No-op if absent.
func (*Tracker) IdleFor ¶
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 ¶
LastActive returns the last-active time for a site and whether one is recorded.
func (*Tracker) Save ¶
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 ¶
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 ¶
Snapshot returns a copy of the last-active map for read-only consumers (the dashboard, diagnostics) without exposing the internal map.