Help us shape the future of Go! Take the 2026 Go Developer Survey

lifecycle

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package lifecycle supervises process readiness, ordered drain and resource ownership. It knows nothing about application modules, transports or configuration sources.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrStarted         = errors.New("lifecycle: already started or closed")
	ErrRunning         = errors.New("lifecycle: work is still running")
	ErrShutdownTimeout = errors.New("lifecycle: shutdown timed out")
)

Functions

This section is empty.

Types

type Config

type Config struct{ StartupTimeout, ShutdownTimeout, CleanupTimeout time.Duration }

Config bounds startup, all drain phases together, and resource cleanup.

type Process

type Process struct {
	Name   string
	Runner Runner
	Phase  int
}

Process names a runner and its drain phase. Lower phases stop first; all runners in the same phase are canceled together before any are joined.

type Runner

type Runner interface {
	Run(context.Context) error
	Ready() <-chan struct{}
}

Runner owns its work until Run returns. Cancellation stops admission and drains or cancels in-flight work. Ready closes only after startup succeeds. Every process implements the same contract, including native library adapters.

func AfterReady

func AfterReady(r Runner, dependencies ...<-chan struct{}) Runner

AfterReady delays a process until all prerequisites are ready. Cancellation while waiting never starts the underlying process.

type Runtime

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

Runtime owns one execution and the resources acquired for it. Register resources before Run, then Close after Run returns. Close refuses to release dependencies while work is active, including work that outlives the shutdown budget. Own is construction-only; Run and Close are safe against concurrent calls.

func New

func New(cfg Config, logger *slog.Logger) (*Runtime, error)

New constructs a lifetime without starting work. Durations must be positive.

func (*Runtime) Close

func (a *Runtime) Close(ctx context.Context) error

Close releases resources once in reverse construction order. Call it after execution returns, with an uncanceled context. It applies the cleanup deadline, preserves context values, and refuses to close resources still in use. Commands must join their goroutines before returning.

func (*Runtime) Own

func (a *Runtime) Own(name string, close func(context.Context) error)

Own is only used during construction, immediately after acquiring a resource.

func (*Runtime) OwnClient

func (a *Runtime) OwnClient(name string, close func() error)

OwnClient adapts native connection Close methods to app cleanup.

func (*Runtime) Ready

func (a *Runtime) Ready() <-chan struct{}

Ready closes after every selected process has become ready.

func (*Runtime) Run

func (a *Runtime) Run(ctx context.Context, processes ...Process) error

Run starts the selected processes and joins them in phase order on cancellation or failure. Normal cancellation returns nil after successful drain. Call Close afterward to release resources. An unexpected process exit fails the group.

func (*Runtime) Stopping

func (a *Runtime) Stopping() <-chan struct{}

Stopping closes before drain begins, including when construction is closed.

Jump to

Keyboard shortcuts

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