Documentation
¶
Overview ¶
Package shutdown provides functionality for graceful application shutdown.
This package implements a robust system for handling application termination, ensuring that resources are properly released and in-flight operations are completed before the application exits. It handles OS signals (SIGINT, SIGTERM, SIGHUP) and context cancellation to trigger graceful shutdown.
Proper shutdown handling is critical for production applications to prevent:
- Data loss from incomplete operations
- Resource leaks from unclosed connections
- Inconsistent state from abrupt termination
- Service disruption for users during deployments
Key features:
- Signal-based shutdown handling (SIGINT, SIGTERM, SIGHUP)
- Context-based shutdown initiation for programmatic control
- Timeout management to prevent hanging during shutdown
- Multiple signal handling with forced exit on second signal
- Comprehensive logging of shutdown events
- Error propagation from shutdown operations
The package provides two main functions:
- GracefulShutdown: Blocks until shutdown is triggered, then executes cleanup
- SetupGracefulShutdown: Sets up background shutdown handling without blocking
Example usage with GracefulShutdown (blocking approach):
func main() {
// Initialize application components
logger := logging.NewContextLogger(zapLogger)
server := startServer()
db := connectToDatabase()
// Define shutdown function
shutdownFunc := func() error {
// Close resources in reverse order of creation
serverErr := server.Shutdown(context.Background())
dbErr := db.Close()
// Return combined error if any
if serverErr != nil || dbErr != nil {
return fmt.Errorf("shutdown errors: server=%v, db=%v", serverErr, dbErr)
}
return nil
}
// Wait for shutdown signal and execute cleanup
if err := shutdown.GracefulShutdown(context.Background(), logger, shutdownFunc); err != nil {
logger.Error(context.Background(), "Shutdown completed with errors", zap.Error(err))
os.Exit(1)
}
}
Example usage with SetupGracefulShutdown (non-blocking approach):
func main() {
// Initialize application components
logger := logging.NewContextLogger(zapLogger)
server := startServer()
db := connectToDatabase()
// Define shutdown function
shutdownFunc := func() error {
// Close resources in reverse order of creation
serverErr := server.Shutdown(context.Background())
dbErr := db.Close()
// Return combined error if any
if serverErr != nil || dbErr != nil {
return fmt.Errorf("shutdown errors: server=%v, db=%v", serverErr, dbErr)
}
return nil
}
// Set up graceful shutdown in the background
cancel, errCh := shutdown.SetupGracefulShutdown(context.Background(), logger, shutdownFunc)
defer cancel() // Ensure shutdown is triggered if main exits
// Continue with application logic
// ...
// Optionally wait for shutdown to complete and check for errors
if err := <-errCh; err != nil {
logger.Error(context.Background(), "Shutdown completed with errors", zap.Error(err))
os.Exit(1)
}
}
The package is designed to be used at the application's entry point (main function) to ensure proper resource cleanup during termination.
Package shutdown provides functionality for graceful application shutdown.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GracefulShutdown ¶
func GracefulShutdown(ctx context.Context, logger *logging.ContextLogger, shutdownFunc func() error) error
GracefulShutdown waits for termination signals and calls the provided shutdown function. It handles OS signals (SIGINT, SIGTERM, SIGHUP) and context cancellation to trigger graceful shutdown. It also handles multiple signals, forcing exit if a second signal is received during shutdown. A default timeout of 30 seconds is applied to the shutdown function to prevent hanging.
Parameters:
- ctx: Context that can be cancelled to trigger shutdown
- logger: Logger for recording shutdown events
- shutdownFunc: Function to execute during shutdown
Returns:
- The error from the shutdown function, if any
func SetupGracefulShutdown ¶
func SetupGracefulShutdown(ctx context.Context, logger *logging.ContextLogger, shutdownFunc func() error) (context.CancelFunc, <-chan error)
SetupGracefulShutdown sets up a goroutine that will handle graceful shutdown. It creates a new context with cancellation and starts a background goroutine that calls GracefulShutdown. This allows for both signal-based and programmatic shutdown initiation.
Parameters:
- ctx: Parent context for the shutdown context
- logger: Logger for recording shutdown events
- shutdownFunc: Function to execute during shutdown
Returns:
- A cancel function that can be called to trigger shutdown programmatically
- A channel that will receive any error that occurs during shutdown
Types ¶
This section is empty.