watch

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2026 License: GPL-2.0, GPL-3.0 Imports: 6 Imported by: 0

Documentation

Overview

Package watch implements the dead-man state machine: it tracks when each configured beat last pinged, declares a beat missing once its deadline of silence passes, and notifies on the missing and recovered transitions.

The deadline clock for every beat starts at construction (process boot), so a beat that never pings at all still alerts one deadline after boot. That deliberately closes the classic dead-man blind spot where a receiver restart silently disarms the switch until the first ping re-arms it.

Index

Constants

View Source
const DefaultTick = 15 * time.Second

DefaultTick is the watch loop's check cadence. Deadlines are minutes to hours, so a fixed 15s sweep bounds alert latency without configuration.

Variables

This section is empty.

Functions

This section is empty.

Types

type Notifier

type Notifier interface {
	// BeatMissing reports that id has been silent for silence (its deadline
	// has passed).
	BeatMissing(ctx context.Context, id string, silence time.Duration) error
	// BeatRecovered reports that id pinged again after having been declared
	// missing, downFor after its last accepted ping.
	BeatRecovered(ctx context.Context, id string, downFor time.Duration) error
}

Notifier delivers the two transition notifications. Implementations are expected to retry transient failures internally and return the final outcome.

type Watcher

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

Watcher tracks beat freshness and drives transition notifications. Beat is safe for concurrent use; Run is the single background sender so notify calls never hold the lock.

func New

func New(beats []config.Beat, notifier Notifier, now func() time.Time) *Watcher

New builds a Watcher for the configured beats. The deadline clock of every beat starts at now(); pass time.Now in production.

func (*Watcher) Beat

func (w *Watcher) Beat(id string) bool

Beat records a ping for id. It returns false when id is not a configured beat (the caller answers 404 and nothing is recorded). A ping on an alerted beat queues the recovered notification for the Run loop, so this never blocks on the webhook.

func (*Watcher) Run

func (w *Watcher) Run(ctx context.Context, tick time.Duration)

Run drives the watch loop until ctx is cancelled: a sweep every tick plus immediate delivery of queued recovered transitions. It is the only goroutine that calls the notifier.

func (*Watcher) Sweep

func (w *Watcher) Sweep(ctx context.Context)

Sweep checks every beat against its deadline and sends the missing notification for newly overdue beats. A failed send is not marked alerted, so the next sweep retries it; the beat stays in one Discord message per outage because alerted flips only on a delivered send. Exported for tests; Run calls it on every tick.

Jump to

Keyboard shortcuts

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