File tree Expand file tree Collapse file tree 2 files changed +5
-6
lines changed
cookbook/graceful-shutdown Expand file tree Collapse file tree 2 files changed +5
-6
lines changed Original file line number Diff line number Diff 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 {
Original file line number Diff line number Diff 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:::
You can’t perform that action at this time.
0 commit comments