runner

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CloseFunc

type CloseFunc func() error

CloseFunc is a simple cleanup handler.

type ReadinessCheck

type ReadinessCheck func(ctx context.Context) error

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 RunConfig

type RunConfig struct {
	Timeout          time.Duration
	Signals          []os.Signal
	ReadinessTimeout time.Duration
}

RunConfig configures the Run behavior.

type RunFunc

type RunFunc func(ctx context.Context) error

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

func WithReadinessTimeout(d time.Duration) RunOption

WithReadinessTimeout sets the timeout for individual readiness checks. Default is 500ms. This should be less than the Kubernetes probe timeoutSeconds.

func WithSignals

func WithSignals(sigs ...os.Signal) RunOption

WithSignals sets the OS signals to listen for (default SIGINT, SIGTERM).

func WithTimeout

func WithTimeout(d time.Duration) RunOption

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 New

func New() *Runner

New creates a new Runner.

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

func (r *Runner) Defer(fns ...CloseFunc)

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

func (r *Runner) Go(fn RunFunc)

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

func (r *Runner) RegisterCtx(fns ...func(ctx context.Context) error)

RegisterCtx is an alias for DeferCtx for compatibility with otel.Registrar interface.

func (*Runner) RegisterHealth

func (r *Runner) RegisterHealth(mux *http.ServeMux, prefix ...string)

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.

func (*Runner) Wait

func (r *Runner) Wait(ctx context.Context, opts ...RunOption) error

Wait blocks until: - An OS signal is received, OR - The parent context is canceled, OR - Any goroutine returns a non-nil error

It then cancels all goroutines, runs cleanup handlers in reverse order, and returns any errors.

type ShutdownFunc

type ShutdownFunc func(ctx context.Context) error

ShutdownFunc is a context-aware cleanup handler.

Jump to

Keyboard shortcuts

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