Documentation
¶
Overview ¶
Package graceful orchestrates the lifecycle of long-running components. A Group starts every registered component together, waits for the context to be cancelled or the first failure, then drains everything in reverse registration order within a bounded timeout. With WithUpgrade a SIGHUP performs a zero-downtime binary upgrade via tableflip: listeners created through Listen are inherited by the new process.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Group ¶
type Group struct {
// contains filtered or unexported fields
}
Group runs components together. Register with Add and Listen, then call Run once; Group is not safe for concurrent registration.
func (*Group) Add ¶
Add registers a component. start runs in a goroutine: it may block for the component's whole life (an accept loop) or return nil after spawning its own work (a scheduler); a non-nil error shuts the whole group down. stop runs during shutdown in reverse registration order, sharing the group's shutdown timeout.
func (*Group) Listen ¶
Listen registers srv to serve on a TCP listener bound to addr. The listener is created by Run — through tableflip under WithUpgrade, so upgraded processes inherit it. http.ErrServerClosed is a clean exit.
func (*Group) Run ¶
Run starts every entry in registration order, then blocks until ctx is cancelled, a component fails, or an upgraded process takes over. It drains the started entries in reverse order and returns the failure that caused the shutdown joined with any drain failures; a requested shutdown that drains cleanly returns nil. Call it once.
type Option ¶
type Option func(*options)
Option configures a Group.
func WithLogger ¶
WithLogger sets the logger for lifecycle events. Default slog.Default().
func WithShutdownTimeout ¶
WithShutdownTimeout bounds the total drain time on shutdown. Default 30s.
func WithUpgrade ¶
func WithUpgrade() Option
WithUpgrade enables zero-downtime binary upgrades on SIGHUP via tableflip. It is a no-op on Windows, where the group falls back to plain listeners.