-
Notifications
You must be signed in to change notification settings - Fork 8.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixes to the graceful shutdown example #2552
Conversation
…ce before the if statement on graceful shutdown
As stated in the
In the graceful shutdown example, the program would exit with |
A project as Gin that is so loved, should really have some more active maintainers.. Can anyone give a comment on this? |
Codecov Report
@@ Coverage Diff @@
## master #2552 +/- ##
=======================================
Coverage 98.64% 98.64%
=======================================
Files 41 41
Lines 1989 1989
=======================================
Hits 1962 1962
Misses 15 15
Partials 12 12 Continue to review full report at Codecov.
|
gin needs additional wrapper for graceful shutdown! Currently, we only have |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm, thanks!
@averageflow Sorry to border you, I just curious about why use go func() {
if err := srv.ListenAndServe(); err != nil && errors.Is(err, http.ErrServerClosed) {
log.Printf("listen: %s\n", err)
}
}() |
@tarupo to be quite honest, I have altered the example to fit my needs and have a better solution. You also should not run things on the same port. here are my utility functions: const (
GracefulShutdownRequestGraceSeconds = 10
)
// ListenAndServeGoRoutine will initiate the listen and serve of the wanted *http.Server and
// respond with a log when the server must be closed.
func ListenAndServeGoRoutine(srv *http.Server) {
err := srv.ListenAndServe()
if err != nil && errors.Is(err, http.ErrServerClosed) {
log.Println("Gracefully closing server..")
}
}
// TerminationSignalWatcher will wait for interrupt signal to gracefully shutdown
// the server with a timeout of x seconds.
func TerminationSignalWatcher(srv *http.Server) {
// Make a channel to receive operating system signals.
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM, syscall.SIGABRT)
<-quit
log.Println("Shutting down server, because of received signal..")
// The context is used to inform the server it has x seconds to finish
// the request it is currently handling
ctx, cancel := context.WithTimeout(
context.Background(),
GracefulShutdownRequestGraceSeconds*time.Second,
)
err := srv.Shutdown(ctx)
if err != nil {
log.Fatal("Server forced to shutdown:", err)
}
defer cancel()
log.Println("Server exiting..")
} and how you could call them in your main: srv := &http.Server{
ReadTimeout: 10 * time.Second,
WriteTimeout: 30 * time.Second,
IdleTimeout: 100 * time.Second,
Addr: ":7001",
Handler: router,
}
// Initialize the server in a goroutine so that
// it won't block the graceful shutdown handling below
go ListenAndServeGoRoutine(srv)
TerminationSignalWatcher(srv) I feel like this is a better example of how you could get it to work, and can recommend it for everyone. I have this working wonderfully in production for several applications. |
@thinkerou maybe you can find some time to add my fonds to the example code and explanation.. i think it is a nicer and cleaner solution. But yes, Go and Gin are already providing you with the puzzle pieces, but i think it is nice for us to showcase it to newcomers |
* Revert "Adding ppc64le architecture support on travis-ci (gin-gonic#2538)" (gin-gonic#2602) * test: fixed the TestUnixSocket test on windows (gin-gonic#2595) Co-authored-by: thinkerou <[email protected]> * gin mode unknown: show available mode (gin-gonic#2567) Co-authored-by: thinkerou <[email protected]> * fix error gin support min Go version (gin-gonic#2584) Co-authored-by: thinkerou <[email protected]> * Fixes to the graceful shutdown example (gin-gonic#2552) * Change error comparison to use errors.Is() and add a line of whitespace before the if statement on graceful shutdown * Change from log.Fatalf to log.Printf to ensure the graceful shutdown actually works Co-authored-by: J. J. Bigorra <[email protected]> Co-authored-by: thinkerou <[email protected]> * basic auth: fix timing oracle (gin-gonic#2609) Co-authored-by: thinkerou <[email protected]> * chore: Deleted spaces (gin-gonic#2622) * Remove the tedious named return value (gin-gonic#2620) Co-authored-by: thinkerou <[email protected]> Co-authored-by: thinkerou <[email protected]> Co-authored-by: Jeff <[email protected]> Co-authored-by: Rubi <[email protected]> Co-authored-by: Qt <[email protected]> Co-authored-by: Josep Jesus Bigorra Algaba <[email protected]> Co-authored-by: J. J. Bigorra <[email protected]> Co-authored-by: Snawoot <[email protected]> Co-authored-by: Alexander Melentyev <[email protected]> Co-authored-by: Andy Pan <[email protected]>
…ce before the if statement on graceful shutdown
master