This example demonstrates how to use lifecycle hooks (WithBeforeShutdown and WithAfterShutdown) to execute custom logic during graceful shutdown.
go run main.goThen send a request:
curl http://localhost:8080/To see the hooks in action, stop the server with Ctrl+C (SIGTERM).
When you stop the server, you should see output like:
Shutdown signal received, starting graceful shutdown...
HOOK: Before shutdown - Notifying load balancer...
HOOK: Load balancer notified
HOOK: Before shutdown - Stopping background workers...
HOOK: Background workers stopped
HOOK: After shutdown - Closing database connections...
HOOK: Database connections closed
HOOK: After shutdown - Flushing metrics...
HOOK: Metrics flushed
Server stopped gracefully-
BeforeShutdown hooks - Execute in registration order before server shutdown begins
- Notify load balancer to stop sending traffic
- Stop background workers
-
Server Shutdown - All HTTP servers shut down gracefully
-
AfterShutdown hooks - Execute in registration order after servers have stopped
- Close database connections
- Flush metrics to external service
- BeforeShutdown: Deregister from service discovery, notify load balancers, stop accepting new work
- AfterShutdown: Close database connections, flush buffers, cleanup resources, send final metrics