appsleep

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package appsleep stops opted-in apps that have had no requests for a while and wakes them on the next request, so idle apps cost no memory or CPU.

Index

Constants

View Source
const (
	SourceHealthProbe = "health probe"
	SourceBot         = "bot"
)

Probe and bot labels for request wakes that look automated.

View Source
const BlockedPinnedPort = "pinned_port"

BlockedPinnedPort marks an app whose pinned host port bypasses the ingress, so nothing could wake it and its activity could not be seen.

View Source
const DefaultInterval = time.Minute

DefaultInterval is how often idle apps are looked for.

Variables

This section is empty.

Functions

func Blocker

func Blocker(svc *store.DesiredService) string

Blocker returns why svc must not sleep, or "" when it can.

func Record

func Record(ctx context.Context, es EventStore, logger *slog.Logger, e store.SleepEvent) int64

Record stores e, and for slept and woke also an app timeline entry. A failure is logged and never returned: the state change already happened. It returns the history row's ID, 0 when nothing was stored.

func Sleep

func Sleep(ctx context.Context, sleep interface {
	SetAppSleeping(ctx context.Context, serviceName string, sleeping bool, now time.Time) error
}, apps Apps, nudger Nudger, name string, now time.Time) error

Sleep stops name now and marks it asleep, rolling the flag back if the stop fails.

func Wake

func Wake(ctx context.Context, sleep interface {
	SetAppSleeping(ctx context.Context, serviceName string, sleeping bool, now time.Time) error
	GetAppSleep(ctx context.Context, serviceName string) (store.AppSleep, error)
}, apps Apps, nudger Nudger, name string, now time.Time) (bool, error)

Wake starts a sleeping app and resets its idle clock. It reports whether the app was actually asleep, so repeated wake requests are harmless.

func WokeEvent

func WokeEvent(name, reason, actor string, slept, now time.Time) store.SleepEvent

WokeEvent builds the history row for a wake. slept is when the app went to sleep.

Types

type Apps

type Apps interface {
	GetDesiredService(ctx context.Context, name string) (*store.DesiredService, error)
	UpdateServiceSuspended(ctx context.Context, name string, suspended bool) error
}

Apps is the slice of the app store the scheduler needs.

type EventStore

type EventStore interface {
	AddSleepEvent(ctx context.Context, e store.SleepEvent) (int64, error)
	AddAppEvent(ctx context.Context, e store.AppEvent) error
}

EventStore persists sleep history and mirrors slept and woke into the app timeline.

type Nudger

type Nudger interface{ Nudge() }

Nudger asks the reconcilers to run now.

type Scheduler

type Scheduler struct {
	Sleep   SleepStore
	Apps    Apps
	Traffic Traffic
	Nudger  Nudger
	// Events records slept and woke history; nil skips it.
	Events EventStore
	Logger *slog.Logger
	Now    func() time.Time
}

Scheduler puts idle opted-in apps to sleep.

func (*Scheduler) Run

func (s *Scheduler) Run(ctx context.Context, interval time.Duration) error

Run ticks on interval until ctx is done.

func (*Scheduler) Tick

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

Tick sleeps every opted-in app idle past its threshold and clears the sleeping flag of any app someone started by hand.

type SleepStore

type SleepStore interface {
	ListAppSleep(ctx context.Context) ([]store.AppSleep, error)
	SetAppSleeping(ctx context.Context, serviceName string, sleeping bool, now time.Time) error
}

SleepStore is the sleep settings table.

type Traffic

type Traffic interface {
	LatestByMetric(ctx context.Context, metric string) ([]telemetry.Sample, error)
}

Traffic reports each app's most recent request sample.

Jump to

Keyboard shortcuts

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