closer

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 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/AddNamed/Close/CloseSync) for the common single-lifecycle application, and an instance API (New / NewWithWait) for callers that need an isolated lifecycle. NewWithWait also installs a signal handler so Wait blocks until SIGINT/SIGTERM (or a custom set) arrives. It mirrors the lavka-promoaction app/cls design.

Usage

Register teardowns as resources are created, wait for a shutdown signal, then close everything in reverse:

cl := closer.NewWithWait() // listens for DefaultSignals

srv := startServer()
cl.AddNamed("http", func() error { return srv.Shutdown(context.Background()) })
cl.AddNamed("db", db.Close)

cl.Wait()              // block until a signal arrives
if err := cl.CloseSync(); err != nil { // LIFO, ordered
    log.Error("shutdown", "err", err)
}

The package-level Add/Close functions operate on a shared global Closer for apps that only need one lifecycle:

closer.AddNamed("db", db.Close)
defer closer.CloseSync()

Close vs CloseSync

CloseSync runs handlers sequentially in strict LIFO order — use it when teardown order matters (it usually does, mirroring construction order). Close runs handlers concurrently with no order guarantee, for independent resources. Both are idempotent, recover panics in handlers, and join all errors. Handlers added after a close are ignored.

Options

  • DefaultSignals: the signal set NewWithWait listens for when none are passed (SIGINT, SIGTERM, SIGQUIT, SIGHUP).
  • NewWithWait(sigs ...os.Signal): override the signal set.

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