heartbeat

package
v0.20.0 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Actor

type Actor struct {
	ID          string        // unique heartbeat identifier
	Interval    time.Duration // expected interval between pings
	Description string        // human‐friendly description of this heartbeat
	Grace       time.Duration // grace period before triggering an alert
	Receivers   []string      // list of receiver IDs to notify upon alerts

	LastBump time.Time // timestamp of the last received heartbeat

	State HeartbeatState // current state (idle, active, grace, missing, etc.)
	// contains filtered or unexported fields
}

Actor handles a single heartbeat’s lifecycle.

func NewActorFromConfig added in v0.14.0

func NewActorFromConfig(cfg ActorConfig) *Actor

NewActorFromConfig creates a new heartbeat actor.

func (*Actor) Mailbox

func (a *Actor) Mailbox() chan<- EventType

Mailbox returns the actor's event channel.

func (*Actor) Run

func (a *Actor) Run(ctx context.Context)

Run starts the actor loop and handles incoming events and timers.

type ActorConfig added in v0.14.0

type ActorConfig struct {
	ID          string                           // unique heartbeat identifier
	Description string                           // human-readable description of the heartbeat
	Interval    time.Duration                    // expected interval between heartbeat pings
	Grace       time.Duration                    // grace period after a missed ping before triggering an alert
	Receivers   []string                         // list of receiver IDs to notify on failure
	Logger      *slog.Logger                     // logger scoped to this actor
	History     *servicehistory.Recorder         // store to persist heartbeat and notification events
	DispatchCh  chan<- notifier.NotificationData // channel to send notifications through the dispatcher
	Metrics     *metrics.Registry                // metrics registry
}

ActorConfig holds all parameters required to construct a heartbeat Actor.

type ActorFactory added in v0.20.0

type ActorFactory interface {
	Build(cfg HeartbeatConfig) (*Actor, error)
}

ActorFactory builds actors from heartbeat configs.

type DefaultActorFactory added in v0.20.0

type DefaultActorFactory struct {
	Logger     *slog.Logger
	History    *servicehistory.Recorder
	Metrics    *metrics.Registry
	DispatchCh chan<- notifier.NotificationData
}

DefaultActorFactory builds actors using the shared dependencies.

func (DefaultActorFactory) Build added in v0.20.0

Build constructs a new Actor without starting it.

type EventType added in v0.20.0

type EventType int

EventType represents a heartbeat event.

const (
	EventReceive EventType = iota // EventReceive is a heartbeat ping.
	EventFail                     // EventFail is a manual failure.
	EventTest                     // EventTest is a test notification.
)

type HeartbeatConfig

type HeartbeatConfig struct {
	ID          string        `json:"id"`
	Description string        `json:"description"`
	Interval    time.Duration `json:"interval"`
	Grace       time.Duration `json:"grace"`
	Receivers   []string      `json:"receivers"`
}

HeartbeatConfig contains heartbeat settings.

type HeartbeatConfigMap

type HeartbeatConfigMap map[string]HeartbeatConfig

HeartbeatConfigMap injects the map key as the ID field into each HeartbeatConfig during YAML unmarshalling.

func (*HeartbeatConfigMap) UnmarshalYAML

func (hcm *HeartbeatConfigMap) UnmarshalYAML(value *yaml.Node) error

UnmarshalYAML parses the map and assigns each key as the ID field of its HeartbeatConfig.

type HeartbeatState added in v0.20.0

type HeartbeatState string

HeartbeatState represents the internal state of a heartbeat actor.

const (
	HeartbeatStateIdle      HeartbeatState = "idle"      // Idle is the heartbeat initial state.
	HeartbeatStateActive    HeartbeatState = "active"    // Active is the heartbeat active state.
	HeartbeatStateGrace     HeartbeatState = "grace"     // Grace is the heartbeat grace state.
	HeartbeatStateMissing   HeartbeatState = "missing"   // Missing is the heartbeat missing state.
	HeartbeatStateFailed    HeartbeatState = "failed"    // Failed is the heartbeat failed state.
	HeartbeatStateRecovered HeartbeatState = "recovered" // Recovered is the heartbeat recovered state. Only used for sending "resolved" messages.
)

func (HeartbeatState) String added in v0.20.0

func (h HeartbeatState) String() string

String returns the HeartbeatState as string.

type Manager

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

Manager routes HTTP pings to Actors.

func NewManagerFromHeartbeatMap added in v0.14.0

func NewManagerFromHeartbeatMap(
	ctx context.Context,
	heartbeatConfigs HeartbeatConfigMap,
	logger *slog.Logger,
	factory ActorFactory,
) (*Manager, error)

NewManagerFromHeartbeatMap creates a Manager from heartbeat config without starting actors.

func (*Manager) Fail added in v0.15.1

func (m *Manager) Fail(id string) error

Fail notifies the Actor with a heartbeat "fail" event.

func (*Manager) Get

func (m *Manager) Get(id string) *Actor

Get returns one heartbeat’s info by ID.

func (*Manager) List

func (m *Manager) List() map[string]*Actor

List returns all configured heartbeats.

func (*Manager) Receive added in v0.15.1

func (m *Manager) Receive(id string) error

Receive notifies the Actor with a heartbeat "receive" event.

func (*Manager) Reconcile added in v0.20.0

func (m *Manager) Reconcile(heartbeatConfigs HeartbeatConfigMap) (ReconcileResult, error)

Reconcile updates the manager with a new config set without rebuilding everything.

func (*Manager) StartAll added in v0.20.0

func (m *Manager) StartAll() int

StartAll starts all actors that are not running yet.

func (*Manager) Test added in v0.15.1

func (m *Manager) Test(id string) error

Test sends a test notification event to the Actor.

type ReconcileResult added in v0.20.0

type ReconcileResult struct {
	Added   int
	Updated int
	Removed int
}

ReconcileResult reports how many actors were added, updated, or removed.

Jump to

Keyboard shortcuts

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