Skip to content

Commit

Permalink
Improved the comments and one of the log messages
Browse files Browse the repository at this point in the history
Improved the comments and one of the log messages to make it clearer for beginners.
  • Loading branch information
zignd authored Mar 21, 2020
1 parent d2224b7 commit 62e9b51
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions graceful-shutdown/graceful-shutdown/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,31 @@ func main() {
Handler: router,
}

// Initializing the server in a goroutine so that
// it won't block the graceful shutdown handling below
go func() {
// service connections
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("listen: %s\n", err)
}
}()

// Wait for interrupt signal to gracefully shutdown the server with
// a timeout of 5 seconds.
quit := make(chan os.Signal, 1)
quit := make(chan os.Signal)
// kill (no param) default send syscall.SIGTERM
// kill -2 is syscall.SIGINT
// kill -9 is syscall.SIGKILL but can't be catch, so don't need add it
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
log.Println("Shutdown Server ...")
log.Println("Shuting down server...")

// The context is used to inform the server it has 5 seconds to finish
// the request it is currently handling
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Fatal("Server Shutdown: ", err)
log.Fatal("Server forced to shutdown:", err)
}

log.Println("Server exiting")
}

0 comments on commit 62e9b51

Please sign in to comment.