health

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2026 License: AGPL-3.0 Imports: 6 Imported by: 0

Documentation

Overview

Package health implements liveness and readiness checks for sqi-server.

Concepts

A Checker represents one dependency that must be reachable for the server to be considered ready. Components register themselves via Registry.Register during startup; later tasks wire in real implementations:

- SQLite store checker - NATS JetStream checker

Endpoints

Registry.LivenessHandler → GET /healthz

Always returns HTTP 200 with {"status":"ok"}. It performs no dependency checks; its only purpose is to prove the process is alive and the HTTP event loop is responding. An orchestrator that receives a non-200 here (or a timeout) will restart the process.

Registry.ReadinessHandler → GET /readyz

Runs all registered checkers concurrently under a five-second deadline. Returns HTTP 200 with {"status":"ok","checks":{…}} when every checker passes, or HTTP 503 with {"status":"degraded","checks":{…}} when any checker fails. If no checkers are registered the server is considered ready immediately — components opt in to readiness gating by calling Register.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Checker

type Checker interface {
	Check(ctx context.Context) error
}

Checker is implemented by any component that wants to gate server readiness. Check must return nil when the component is healthy and a descriptive, human-readable error when it is not. Implementations must honor context cancellation and return promptly when ctx is done.

type CheckerFunc

type CheckerFunc func(ctx context.Context) error

CheckerFunc is a function that implements Checker. It allows an inline closure to be registered without defining a named type.

reg.Register("sqlite", health.CheckerFunc(func(ctx context.Context) error {
    return db.PingContext(ctx)
}))

func (CheckerFunc) Check

func (f CheckerFunc) Check(ctx context.Context) error

Check implements Checker.

type Registry

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

Registry holds a set of named [Checker]s consulted by Registry.ReadinessHandler. It is safe for concurrent use; components may call Registry.Register from any goroutine, including during server startup.

func NewRegistry

func NewRegistry() *Registry

NewRegistry returns an empty Registry.

func (*Registry) LivenessHandler

func (*Registry) LivenessHandler() http.Handler

LivenessHandler returns an http.Handler for GET /healthz.

It always responds HTTP 200 with {"status":"ok"} and performs no dependency checks. Use this as a process-alive probe — if the handler stops responding the process should be restarted.

func (*Registry) ReadinessHandler

func (r *Registry) ReadinessHandler() http.Handler

ReadinessHandler returns an http.Handler for GET /readyz.

It runs all registered checkers concurrently under a [checkTimeout] deadline derived from the incoming request context. The response body always includes a per-checker breakdown regardless of overall status.

HTTP 200 {"status":"ok","checks":{…}} — all checkers passed. HTTP 503 {"status":"degraded","checks":{…}} — one or more checkers failed.

func (*Registry) Register

func (r *Registry) Register(name string, c Checker)

Register adds a named Checker to the registry. If a checker with the same name already exists it is replaced. Safe to call concurrently.

Jump to

Keyboard shortcuts

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