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.
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.
Example ¶
CloseSync runs the teardowns in reverse: the server stops before the pool it uses. Errors are joined, and a second close does nothing.
package main
import (
"errors"
"fmt"
"github.com/assanoff/skit/closer"
)
func main() {
cl := closer.New()
cl.AddNamed("db", func() error {
fmt.Println("close db")
return nil
})
cl.AddNamed("cache", func() error {
fmt.Println("close cache")
return errors.New("cache: connection reset")
})
cl.AddNamed("http", func() error {
fmt.Println("stop http")
return nil
})
fmt.Println("err:", cl.CloseSync())
fmt.Println("again:", cl.CloseSync())
}
Output: stop http close cache close db err: cache: connection reset again: <nil>
Index ¶
Examples ¶
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.
Example ¶
A service registers its teardowns as it builds the resources, waits for SIGINT or SIGTERM, then closes them in reverse.
package main
import (
"context"
"log/slog"
"net/http"
"github.com/assanoff/skit/closer"
)
func main() {
cl := closer.NewWithWait() // DefaultSignals
srv := &http.Server{Addr: ":8080"}
go func() { _ = srv.ListenAndServe() }()
cl.AddNamed("http", func() error {
return srv.Shutdown(context.Background())
})
cl.Wait()
if err := cl.CloseSync(); err != nil {
slog.Error("shutdown", "err", err)
}
}
Output:
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.
Example ¶
Close runs independent teardowns at once, in no order, and joins their errors.
package main
import (
"errors"
"fmt"
"sync/atomic"
"github.com/assanoff/skit/closer"
)
func main() {
cl := closer.New()
var closed atomic.Int32
for range 3 {
cl.Add(func() error {
closed.Add(1)
return nil
})
}
cl.Add(func() error { return errors.New("broker: timeout") })
fmt.Println("err:", cl.Close())
fmt.Println("closed:", closed.Load())
}
Output: err: broker: timeout closed: 3