Fix Shutdown() sample

With current version of `Shutdown()` sample code, server is always blocked by 5 seconds timeout. We shouldn't wait for `ctx.Done()`.

see: https://github.com/gin-gonic/examples/blob/master/graceful-shutdown/graceful-shutdown/server.go#L46-L51
This commit is contained in:
Ryo Utsunomiya 2019-09-24 16:22:23 +09:00 committed by GitHub
parent 2e5a7196cc
commit 6995a1ea7e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1759,10 +1759,10 @@ func main() {
// Wait for interrupt signal to gracefully shutdown the server with
// a timeout of 5 seconds.
quit := make(chan os.Signal)
// kill (no param) default send syscall.SIGTERM
quit := make(chan os.Signal, 1)
// kill (no param) default send syscanll.SIGTERM
// kill -2 is syscall.SIGINT
// kill -9 is syscall.SIGKILL but can't be catch, so don't need add it
// 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 ...")
@ -1772,11 +1772,7 @@ func main() {
if err := srv.Shutdown(ctx); err != nil {
log.Fatal("Server Shutdown: ", err)
}
// catching ctx.Done(). timeout of 5 seconds.
select {
case <-ctx.Done():
log.Println("timeout of 5 seconds.")
}
log.Println("Server exiting")
}
```