closer

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package closer manages graceful shutdown of resources. Cleanup handlers run in LIFO order (last registered, first closed), matching resource dependency order. It exposes both a process-global default (Add/Close/CloseSync) for the common single-lifecycle application, and an instance API (New/NewWithWait) for callers that need an isolated lifecycle.

It mirrors the lavka-promoaction app/cls design.

Index

Constants

This section is empty.

Variables

DefaultSignals is the default list of signals NewWithWait listens for.

Functions

func Add

func Add(fn func() error)

Add adds a shutdown function to the global closer.

func AddNamed

func AddNamed(name string, fn func() error)

AddNamed adds a named shutdown function to the global closer.

func Close

func Close() error

Close closes all functions in the global closer concurrently (no order guarantee).

func CloseSync

func CloseSync() error

CloseSync closes all functions in the global closer sequentially in LIFO order.

Types

type Closer

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

Closer manages graceful shutdown of resources, executing registered cleanup handlers in LIFO (Last-In-First-Out) order.

func New

func New() *Closer

New creates a new Closer.

func NewWithWait

func NewWithWait(sigs ...os.Signal) *Closer

NewWithWait creates a Closer and starts a goroutine that blocks until one of the given signals arrives (DefaultSignals when none are passed). Wait blocks until that happens.

func (*Closer) Add

func (c *Closer) Add(fn func() error)

Add adds a shutdown function. Handlers run in LIFO order on Close/CloseSync. A nil fn is ignored.

func (*Closer) AddNamed

func (c *Closer) AddNamed(name string, fn func() error)

AddNamed adds a shutdown function with a name used in logging.

func (*Closer) Close

func (c *Closer) Close() error

Close executes the handlers asynchronously (concurrently). It does NOT guarantee LIFO order; use CloseSync when order matters.

func (*Closer) CloseSync

func (c *Closer) CloseSync() error

CloseSync executes the handlers sequentially in LIFO order.

func (*Closer) Wait

func (c *Closer) Wait()

Wait blocks until the signal goroutine (from NewWithWait) returns.

Jump to

Keyboard shortcuts

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