Skip to content

Commit e9b7e42

Browse files
eyasy1217aldas
authored andcommitted
Updated graceful shutdown implementation to align with Go v1.16+
1 parent 803f9c0 commit e9b7e42

File tree

2 files changed

+5
-6
lines changed

2 files changed

+5
-6
lines changed

cookbook/graceful-shutdown/server.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,17 @@ func main() {
2020
return c.JSON(http.StatusOK, "OK")
2121
})
2222

23+
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
24+
defer stop()
2325
// Start server
2426
go func() {
2527
if err := e.Start(":1323"); err != nil && err != http.ErrServerClosed {
2628
e.Logger.Fatal("shutting down the server")
2729
}
2830
}()
2931

30-
// Wait for interrupt signal to gracefully shutdown the server with a timeout of 10 seconds.
31-
// Use a buffered channel to avoid missing signals as recommended for signal.Notify
32-
quit := make(chan os.Signal, 1)
33-
signal.Notify(quit, os.Interrupt)
34-
<-quit
32+
// Wait for interrupt signal to gracefully shutdown the server with a timeout of 10 seconds.
33+
<-ctx.Done()
3534
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
3635
defer cancel()
3736
if err := e.Shutdown(ctx); err != nil {

website/docs/cookbook/graceful-shutdown.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ https://github.com/labstack/echox/blob/master/cookbook/graceful-shutdown/server.
1212

1313
:::note
1414

15-
Requires go1.8+
15+
Requires go1.16+
1616

1717
:::

0 commit comments

Comments
 (0)