app

package
v0.9.9 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2026 License: Apache-2.0 Imports: 5 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package app owns the process lifecycle: resources start together and close in reverse order, so a worker that fails cannot suppress the cleanup of the connections it was using.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrLifecycleClosed reports an attempt to add work after shutdown began.
	ErrLifecycleClosed = errors.New("application lifecycle is closed")
	// ErrLifecycleStarted reports an attempt to add work after waiting began.
	ErrLifecycleStarted = errors.New("application lifecycle has already started")
)

Functions

func Shutdown

func Shutdown(ctx context.Context, resources ...Resource) error

Shutdown closes every resource, retaining all failures instead of stopping at the first one. A failed watcher must not leave the worker or database open, and a failed worker must not suppress connection cleanup.

Types

type CloseFunc

type CloseFunc func(context.Context) error

CloseFunc closes one application-owned resource. The context bounds work such as a worker's graceful SHUTDOWN; resources that do not need it can ignore it through Lifecycle.AddCloser.

type Lifecycle

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

Lifecycle coordinates long-lived loops and their owned resources. A caller registers resources in shutdown order: MCP ingress, watcher, worker, connections, and LadybugDB/storage. Run loops are stopped by cancelling Context and are waited after resource close.

func NewLifecycle

func NewLifecycle(parent context.Context) *Lifecycle

NewLifecycle creates an application lifecycle rooted at parent. A nil parent is treated as context.Background.

func (*Lifecycle) Add

func (lifecycle *Lifecycle) Add(resource Resource) error

Add registers a close operation. Resources must be added in the order they must close, not in reverse construction order.

func (*Lifecycle) AddCloser

func (lifecycle *Lifecycle) AddCloser(name string, closer interface{ Close() error }) error

AddCloser registers a conventional Close() error resource.

func (*Lifecycle) AddContextCloser

func (lifecycle *Lifecycle) AddContextCloser(name string, closer interface {
	Close(context.Context) error
}) error

AddContextCloser registers a resource whose close operation accepts a context, such as tsworker.Supervisor.

func (*Lifecycle) Context

func (lifecycle *Lifecycle) Context() context.Context

Context is the cancellation context shared by all long-lived loops.

func (*Lifecycle) Go

func (lifecycle *Lifecycle) Go(name string, run RunFunc) error

Go starts one long-lived loop. Calls after Wait or Shutdown are rejected so the wait group cannot be extended while it is being drained.

func (*Lifecycle) Shutdown

func (lifecycle *Lifecycle) Shutdown(ctx context.Context) error

Shutdown cancels all loops, closes resources in registration order, and waits for the loops to exit. It is idempotent and safe for concurrent callers. The first caller supplies the context used by close operations; later callers may use a different context to bound their own wait for that same shutdown.

func (*Lifecycle) Wait

func (lifecycle *Lifecycle) Wait() error

Wait waits for all registered loops and returns their unexpected failures. It does not close resources; Shutdown must be used by the owner.

type Resource

type Resource struct {
	Name  string
	Close CloseFunc
}

Resource is one close operation in shutdown order. Resources are closed in the order supplied to Shutdown: ingress first, then its producers and finally storage. That ordering is intentional; reverse-registration order would let new work arrive while its dependencies were already closing.

type RunFunc

type RunFunc func(context.Context) error

RunFunc is one long-lived application loop. Its context is cancelled before shutdown resources are closed.

Jump to

Keyboard shortcuts

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