Skip to content

Commit

Permalink
Updated graceful shutdown implementation to align with Go v1.16+
Browse files Browse the repository at this point in the history
  • Loading branch information
eyasy1217 authored and aldas committed Jan 25, 2024
1 parent 803f9c0 commit e9b7e42
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
9 changes: 4 additions & 5 deletions cookbook/graceful-shutdown/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,17 @@ 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 {
e.Logger.Fatal("shutting down the server")
}
}()

// 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 {
Expand Down
2 changes: 1 addition & 1 deletion website/docs/cookbook/graceful-shutdown.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ https://github.com/labstack/echox/blob/master/cookbook/graceful-shutdown/server.

:::note

Requires go1.8+
Requires go1.16+

:::

0 comments on commit e9b7e42

Please sign in to comment.