shutdown
Phased graceful shutdown for services, parallel within a phase, with a watchdog and telemetry hooks.
go get github.com/ubgo/shutdown
- Language
- Go
- Go version
- 1.24
- Core deps
- 0 direct
- Latest tag
- v0.2.0
- License
- Apache-2.0
- Go modules
- 9
How it fits together
Code from the README
mgr := shutdown.New(shutdown.WithBudget(30 * time.Second))
srv := &http.Server{Addr: ":8080", Handler: r}
_ = shutdownnethttp.Register(mgr, srv)
_ = mgr.Register("db", closeFn(db.Close), shutdown.WithPhase(shutdown.PhaseCloseClients))
_ = mgr.Register("redis", closeFn(rdb.Close), shutdown.WithPhase(shutdown.PhaseCloseClients))
go func() { _ = srv.ListenAndServe() }()
_ = mgr.Listen(ctx)stop := make(chan struct{})
handle, _ := mgr.RegisterActor("worker", func(_ error) {
close(stop) // tell the run loop to exit
}, shutdown.WithActorPhase(shutdown.PhaseDrainTraffic))
go func() {
err := worker.Run(stop) // blocks until stop is closed
handle.Done(err) // tells the manager the actor finished
}()mux.HandleFunc("/admin/shutdown", func(w http.ResponseWriter, r *http.Request) {
if !authorized(r) { w.WriteHeader(http.StatusForbidden); return }
go func() {
_ = mgr.Shutdown(context.Background()) // same execution path as a signal
}()
w.WriteHeader(http.StatusAccepted)
})mgr.OnSignal(syscall.SIGHUP, func(ctx context.Context, _ os.Signal) {
if err := config.Reload(); err != nil {
log.Println("reload failed:", err)
}
})
mgr.OnSignal(syscall.SIGUSR1, func(ctx context.Context, _ os.Signal) {
rotateLogFile()
})From the shutdown README.
At a glance (from the README)
| Adapter | Module path | Role |
|---|---|---|
shutdown-nethttp | github.com/ubgo/shutdown/contrib/shutdown-nethttp | *http.Server.Shutdown registered as a phase handler |
shutdown-gin | github.com/ubgo/shutdown/contrib/shutdown-gin | Same, for the *http.Server wrapping a Gin engine |
shutdown-chi | github.com/ubgo/shutdown/contrib/shutdown-chi | Same, for the *http.Server wrapping a Chi router |
shutdown-echo | github.com/ubgo/shutdown/contrib/shutdown-echo | *echo.Echo.Shutdown (Echo owns its server) |
shutdown-fiber | github.com/ubgo/shutdown/contrib/shutdown-fiber | *fiber.App.ShutdownWithContext |
shutdown-zap | github.com/ubgo/shutdown/contrib/shutdown-zap | Observer that emits structured logs via go.uber.org/zap |
shutdown-otel | github.com/ubgo/shutdown/contrib/shutdown-otel | Observer that emits OpenTelemetry spans (root + phase + handler) |
shutdown-prom | github.com/ubgo/shutdown/contrib/shutdown-prom | Observer that exports Prometheus metrics |