Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddHandler ¶
func AddHandler(handler Handler)
func NewContext ¶ added in v0.8.0
NewContext creates a root, shutdown-aware context for the entire program.
In any Go application, the root context serves as the base for all other derived contexts. It is typically used to propagate cancellation signals and deadlines across multiple goroutines and services. `NewContext` ensures that the root context is properly initialized and protected from multiple concurrent creations.
This function wraps `WithContext(context.Background())` and returns a cancelable context that will be automatically canceled when the application initiates a shutdown, either manually via `Shutdown()`/`Exit()` or due to system signals (e.g., SIGINT, SIGTERM). Using this root context as the base for all other contexts ensures consistent handling of shutdown across the program.
Example usage:
ctx, err := shutdown.NewContext()
if err != nil {
panic(err)
}
// pass `ctx` to servers, workers, or any long-running routines
By standardizing the creation of a root shutdown context, the application can gracefully cancel all operations and clean up resources consistently when shutting down.