health

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: MIT Imports: 9 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 health is the single supervisor: it starts restartable components, watches their state and heartbeat, restarts them within a budget and cancels the root context with ErrHealthFatal when that is no longer possible (spec 8.2).

Index

Constants

View Source
const (
	DeviceOnline       = "online"
	DeviceOffline      = "offline"
	DeviceStale        = "stale"
	DeviceUnconfigured = "unconfigured"
)

Device states (spec 8.2).

View Source
const (
	RestartBudget = 3
	StopTimeout   = 10 * time.Second
)

Limits of the supervisor.

Variables

View Source
var ErrHealthFatal = errors.New("health: fatal")

ErrHealthFatal cancels the root context: budget exhausted, a Stop timed out or a non-restartable part failed.

Functions

This section is empty.

Types

type Component

type Component interface {
	Start(ctx context.Context) error // error only for unrecoverable conditions
	Stop() error                     // idempotent, graceful, bounded
	State() State
	LastBeat() time.Time
}

Component is a restartable background component.

type ComponentStatus

type ComponentStatus struct {
	Name     string    `json:"name"`
	State    string    `json:"state"`
	Restarts int       `json:"restarts"`
	LastBeat time.Time `json:"last_beat"`
}

ComponentStatus is one row of the snapshot.

type Factory

type Factory func() (Component, error)

Factory builds a fresh component instance; restart = Stop → Factory → Start.

type Poll

type Poll struct {
	At   time.Time
	Took time.Duration
	OK   bool
}

Poll is the last poll of a device.

type Reporter

type Reporter interface {
	DeviceState(device, state string)
	// DevicePoll reports the last poll of a device: when it started, how long it took from
	// the first request to the decoded sample, and whether it succeeded.
	DevicePoll(device string, poll Poll)
}

Reporter receives device health from collectors.

type Snapshot

type Snapshot struct {
	At         time.Time // time of the last sweep
	Components []ComponentStatus
	Devices    map[string]string
	Polls      map[string]Poll
	Fatal      bool
}

Snapshot is the health view for GET /health.

type State

type State int

State of a component.

const (
	Healthy State = iota
	Recovering
	Failed
)

Component states.

func (State) String

func (s State) String() string

type Supervisor

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

Supervisor owns all restartable components.

func NewSupervisor

func NewSupervisor(clock util.Clock, sweep time.Duration, cancel context.CancelCauseFunc,
	log zerolog.Logger,
) (*Supervisor, error)

NewSupervisor builds a supervisor; cancel is the root context's cancel function.

func (*Supervisor) Add

func (s *Supervisor) Add(name string, f Factory, grace time.Duration)

Add registers a component; it is built and started by Run.

func (*Supervisor) DevicePoll

func (s *Supervisor) DevicePoll(device string, poll Poll)

DevicePoll implements Reporter.

func (*Supervisor) DeviceState

func (s *Supervisor) DeviceState(device, state string)

DeviceState implements Reporter.

func (*Supervisor) Fail

func (s *Supervisor) Fail(err error)

Fail reports a failure of a non-restartable part (storage, event bus, HTTP server).

func (*Supervisor) ForgetDevice

func (s *Supervisor) ForgetDevice(device string)

ForgetDevice drops the state and last poll of a device that left the configuration.

func (*Supervisor) Names

func (s *Supervisor) Names() []string

Names returns the names of all components.

func (*Supervisor) Remove

func (s *Supervisor) Remove(name string)

Remove stops and removes a component; unknown names are ignored.

func (*Supervisor) Run

func (s *Supervisor) Run(ctx context.Context) error

Run starts every component and sweeps until ctx ends, then stops them all.

func (*Supervisor) Set

func (s *Supervisor) Set(name string, f Factory, grace time.Duration)

Set registers a component or replaces the one with the same name (configuration change, spec 3): the old instance is stopped, the new one is built and started at once when Run is active, with a fresh restart budget.

func (*Supervisor) Snapshot

func (s *Supervisor) Snapshot() Snapshot

Snapshot returns the current state without blocking on components.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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