Documentation
¶
Index ¶
- type CloseFunc
- type ReadinessCheck
- type RunConfig
- type RunFunc
- type RunOption
- type Runner
- func (r *Runner) AddReadinessCheck(name string, check ReadinessCheck)
- func (r *Runner) Defer(fns ...CloseFunc)
- func (r *Runner) DeferCtx(fns ...ShutdownFunc)
- func (r *Runner) Go(fn RunFunc)
- func (r *Runner) Recover()
- func (r *Runner) RegisterCtx(fns ...func(ctx context.Context) error)
- func (r *Runner) RegisterHealth(mux *http.ServeMux, prefix ...string)
- func (r *Runner) Wait(ctx context.Context, opts ...RunOption) error
- type ShutdownFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ReadinessCheck ¶
ReadinessCheck is a named health check function. It should return nil if healthy, or an error describing the failure. The context has a timeout; implementations should respect cancellation.
type RunFunc ¶
RunFunc is a long-running goroutine that should return nil on clean exit or a non-nil error if it fails and should trigger shutdown. It must respect ctx cancellation for graceful stopping.
type RunOption ¶
type RunOption func(*RunConfig)
RunOption configures Run.
func WithReadinessTimeout ¶
WithReadinessTimeout sets the timeout for individual readiness checks. Default is 500ms. This should be less than the Kubernetes probe timeoutSeconds.
func WithSignals ¶
WithSignals sets the OS signals to listen for (default SIGINT, SIGTERM).
func WithTimeout ¶
WithTimeout sets the shutdown timeout (default 30s).
type Runner ¶
type Runner struct {
// contains filtered or unexported fields
}
Runner manages long-running goroutines and cleanup handlers. Goroutines registered with Go() start immediately. Run() blocks until OS signals or errors, then triggers cleanup.
func (*Runner) AddReadinessCheck ¶
func (r *Runner) AddReadinessCheck(name string, check ReadinessCheck)
AddReadinessCheck registers a named readiness check. All checks run in parallel when /health/ready is called. Each check has a timeout (default 500ms, configurable via WithReadinessTimeout).
Panics if name is empty or check is nil. If a check with the same name already exists, it is replaced.
func (*Runner) Defer ¶
Defer adds cleanup functions to be called during shutdown. They are called in reverse order of registration.
func (*Runner) DeferCtx ¶
func (r *Runner) DeferCtx(fns ...ShutdownFunc)
DeferCtx adds context-aware cleanup functions to be called during shutdown. They are called in reverse order of registration.
func (*Runner) Go ¶
Go starts a long-running goroutine immediately. The function should return nil on clean exit or an error to trigger shutdown. It must respect ctx cancellation for graceful stopping.
func (*Runner) Recover ¶
func (r *Runner) Recover()
Recover logs any panic that occurs in the calling goroutine.
func (*Runner) RegisterCtx ¶
RegisterCtx is an alias for DeferCtx for compatibility with otel.Registrar interface.
func (*Runner) RegisterHealth ¶
RegisterHealth registers health check endpoints on the provided mux.
Endpoints:
- GET <prefix>/live - liveness probe (is the process alive?)
- GET <prefix>/ready - readiness probe (can it receive traffic?)
- GET <prefix>/startup - startup probe (has initialization completed?)
The default prefix is "/health". Pass a custom prefix as the second argument. Note: Uses Go 1.22+ ServeMux method patterns ("GET /path"). The started state is automatically set when Runner.Wait() is called.
type ShutdownFunc ¶
ShutdownFunc is a context-aware cleanup handler.