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
- Variables
- type Component
- type ComponentStatus
- type Factory
- type Poll
- type Reporter
- type Snapshot
- type State
- type Supervisor
- func (s *Supervisor) Add(name string, f Factory, grace time.Duration)
- func (s *Supervisor) DevicePoll(device string, poll Poll)
- func (s *Supervisor) DeviceState(device, state string)
- func (s *Supervisor) Fail(err error)
- func (s *Supervisor) ForgetDevice(device string)
- func (s *Supervisor) Names() []string
- func (s *Supervisor) Remove(name string)
- func (s *Supervisor) Run(ctx context.Context) error
- func (s *Supervisor) Set(name string, f Factory, grace time.Duration)
- func (s *Supervisor) Snapshot() Snapshot
Constants ¶
const ( DeviceOnline = "online" DeviceOffline = "offline" DeviceStale = "stale" DeviceUnconfigured = "unconfigured" )
Device states (spec 8.2).
const ( RestartBudget = 3 StopTimeout = 10 * time.Second )
Limits of the supervisor.
Variables ¶
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 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 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.