From e9b7e42a5073d512f2d3704937d1b448bd5b647e Mon Sep 17 00:00:00 2001 From: eyasy1217 Date: Thu, 25 Jan 2024 22:55:05 +0900 Subject: [PATCH] Updated graceful shutdown implementation to align with Go v1.16+ --- cookbook/graceful-shutdown/server.go | 9 ++++----- website/docs/cookbook/graceful-shutdown.md | 2 +- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/cookbook/graceful-shutdown/server.go b/cookbook/graceful-shutdown/server.go index 03be5122..45ecef1a 100644 --- a/cookbook/graceful-shutdown/server.go +++ b/cookbook/graceful-shutdown/server.go @@ -20,6 +20,8 @@ func main() { return c.JSON(http.StatusOK, "OK") }) + ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt) + defer stop() // Start server go func() { if err := e.Start(":1323"); err != nil && err != http.ErrServerClosed { @@ -27,11 +29,8 @@ func main() { } }() - // Wait for interrupt signal to gracefully shutdown the server with a timeout of 10 seconds. - // Use a buffered channel to avoid missing signals as recommended for signal.Notify - quit := make(chan os.Signal, 1) - signal.Notify(quit, os.Interrupt) - <-quit + // Wait for interrupt signal to gracefully shutdown the server with a timeout of 10 seconds. + <-ctx.Done() ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() if err := e.Shutdown(ctx); err != nil { diff --git a/website/docs/cookbook/graceful-shutdown.md b/website/docs/cookbook/graceful-shutdown.md index 4a935324..b54dbc42 100644 --- a/website/docs/cookbook/graceful-shutdown.md +++ b/website/docs/cookbook/graceful-shutdown.md @@ -12,6 +12,6 @@ https://github.com/labstack/echox/blob/master/cookbook/graceful-shutdown/server. :::note -Requires go1.8+ +Requires go1.16+ :::