healthcheck

package
v0.1.0-beta.49 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Enabled  bool          // Whether health monitoring is enabled
	Interval time.Duration // Check interval (e.g., 15s)
	Fall     int           // Mark unhealthy after N consecutive failures
	Rise     int           // Mark healthy after N consecutive successes
	Timeout  time.Duration // Per-check timeout
}

Config holds the health monitor configuration.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns the default health monitor configuration.

type ConfigUpdater

type ConfigUpdater interface {
	OnHealthChange(healthyTargets []Target)
}

ConfigUpdater is an interface for updating proxy config when health state changes.

type HTTPChecker

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

HTTPChecker performs HTTP health checks on targets.

func NewHTTPChecker

func NewHTTPChecker(timeout time.Duration) *HTTPChecker

NewHTTPChecker creates a new HTTP health checker with the given timeout.

func (*HTTPChecker) Check

func (c *HTTPChecker) Check(ctx context.Context, target Target) Result

Check performs a health check on the given target. A target is considered healthy if the HTTP request succeeds with a 2xx or 3xx status code.

func (*HTTPChecker) CheckAll

func (c *HTTPChecker) CheckAll(ctx context.Context, targets []Target, maxConcurrent int) []Result

CheckAll performs health checks on all targets concurrently. It limits concurrency to maxConcurrent to avoid overwhelming the system.

func (*HTTPChecker) CheckWithRetry

func (c *HTTPChecker) CheckWithRetry(ctx context.Context, target Target, config RetryConfig, onRetry func(attempt int, backoff time.Duration)) Result

CheckWithRetry performs a health check with exponential backoff retries. This is used during initial deployment when containers may take time to start. The onRetry callback is called before each retry attempt (can be nil).

type HealthMonitor

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

HealthMonitor runs continuous health checks in the background.

func NewHealthMonitor

func NewHealthMonitor(
	config Config,
	targetProvider TargetProvider,
	configUpdater ConfigUpdater,
	logger *slog.Logger,
) *HealthMonitor

NewHealthMonitor creates a new health monitor.

func (*HealthMonitor) ForceCheck

func (m *HealthMonitor) ForceCheck()

ForceCheck triggers an immediate health check round, bypassing the ticker. This is useful after deployment changes to quickly update health state.

func (*HealthMonitor) GetHealthyTargets

func (m *HealthMonitor) GetHealthyTargets() []Target

GetHealthyTargets returns the current list of healthy targets.

func (*HealthMonitor) GetStats

func (m *HealthMonitor) GetStats() (total, healthy, unhealthy int)

GetStats returns current health statistics.

func (*HealthMonitor) Start

func (m *HealthMonitor) Start()

Start begins the health monitoring loop. It is safe to call Start multiple times; subsequent calls are no-ops.

func (*HealthMonitor) Stop

func (m *HealthMonitor) Stop()

Stop stops the health monitoring loop and waits for it to finish.

type Result

type Result struct {
	Target  Target
	Healthy bool
	Err     error
	Latency time.Duration
}

Result represents the outcome of a single health check.

type RetryConfig

type RetryConfig struct {
	MaxRetries     int           // Maximum number of retry attempts
	InitialBackoff time.Duration // Initial backoff duration
	MaxBackoff     time.Duration // Maximum backoff duration
}

RetryConfig holds configuration for retry behavior.

func DefaultRetryConfig

func DefaultRetryConfig() RetryConfig

DefaultRetryConfig returns the default retry configuration for initial deployment.

type StateTracker

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

StateTracker tracks health state for multiple targets with fall/rise thresholds. It follows the HAProxy/Traefik pattern: - fall: Mark unhealthy after N consecutive failures - rise: Mark healthy after N consecutive successes

func NewStateTracker

func NewStateTracker(fall, rise int) *StateTracker

NewStateTracker creates a new state tracker with the given thresholds.

func (*StateTracker) GetAllTargets

func (st *StateTracker) GetAllTargets() []Target

GetAllTargets returns all tracked targets regardless of state.

func (*StateTracker) GetHealthyTargets

func (st *StateTracker) GetHealthyTargets() []Target

GetHealthyTargets returns all targets currently in healthy state.

func (*StateTracker) GetState

func (st *StateTracker) GetState(targetID string) TargetState

GetState returns the current state of a target by ID. Returns StateUnhealthy if the target is not found.

func (*StateTracker) GetStats

func (st *StateTracker) GetStats() (total, healthy, unhealthy int)

GetStats returns statistics about the tracked targets.

func (*StateTracker) GetUnhealthyTargets

func (st *StateTracker) GetUnhealthyTargets() []Target

GetUnhealthyTargets returns all targets currently in unhealthy state.

func (*StateTracker) RecordResult

func (st *StateTracker) RecordResult(result Result) (stateChanged bool)

RecordResult updates the state based on a health check result. Returns true if the target's state changed (healthy->unhealthy or vice versa).

func (*StateTracker) SyncTargets

func (st *StateTracker) SyncTargets(targets []Target) bool

SyncTargets updates the tracked targets to match the provided list. New targets are added with initial healthy state, stale targets are removed. Returns true if any targets were added or removed.

type Target

type Target struct {
	ID              string // Container ID
	AppName         string
	IP              string
	Port            string
	HealthCheckPath string // e.g., "/health"
}

Target represents a backend to health check.

type TargetProvider

type TargetProvider interface {
	GetHealthCheckTargets() []Target
}

TargetProvider is an interface for getting health check targets.

type TargetState

type TargetState int

TargetState represents the current health state of a target.

const (
	StateHealthy   TargetState = iota // Target is healthy and receiving traffic
	StateUnhealthy                    // Target is unhealthy and not receiving traffic
)

func (TargetState) String

func (s TargetState) String() string

Jump to

Keyboard shortcuts

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