From 5fefa8af6298d5cc62c996dc8611c2224bb30471 Mon Sep 17 00:00:00 2001 From: PratikDhanave Date: Sun, 1 Nov 2020 16:05:04 +0530 Subject: [PATCH] graceful shutdown allows to server to wait for 10 sec to finish work --- main.go | 53 +++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 47 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index 2e6cef7..cdb1398 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,13 @@ package main import ( + "context" + "log" + "net/http" + "os" + "os/signal" + "syscall" + "time" "ukiyo/pkg/apilayer" "ukiyo/pkg/webhooks" @@ -8,10 +15,44 @@ import ( ) func main() { - r := gin.Default() - webhooks.HooksListener(r) - webhooks.HealthCheck(r) - apilayer.AddContainer(r) - apilayer.EditContainerToken(r) - r.Run(":8080") + router := gin.Default() + + webhooks.HooksListener(router) + webhooks.HealthCheck(router) + apilayer.AddContainer(router) + apilayer.EditContainerToken(router) + + srv := &http.Server{ + Addr: ":8080", + Handler: router, + } + + // Initializing the server in a goroutine so that + // it won't block the graceful shutdown handling below + go func() { + 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) + // 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("ukiyo Shutting down server...") + + // The context is used to inform the server it has 10 seconds to finish + // the request it is currently handling + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + if err := srv.Shutdown(ctx); err != nil { + log.Fatal("ukiyo Server forced to shutdown:", err) + } + + log.Println("ukiyo Server exiting") + }