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 ¶
var DefaultSignals = []os.Signal{ syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGHUP, }
DefaultSignals is the default list of signals NewWithWait listens for.
Functions ¶
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 NewWithWait ¶
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 ¶
Add adds a shutdown function. Handlers run in LIFO order on Close/CloseSync. A nil fn is ignored.
func (*Closer) Close ¶
Close executes the handlers asynchronously (concurrently). It does NOT guarantee LIFO order; use CloseSync when order matters.