From 42c290ce9f95c324735bd6a5370819dde4a87263 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin <valyala@gmail.com> Date: Fri, 27 Mar 2020 20:08:42 +0200 Subject: [PATCH] lib/httpserver: add `-http.maxGracefulShutdownDuration` command-line flag for tuning the maximum duration required for graceful shutdown of http server --- lib/httpserver/httpserver.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/httpserver/httpserver.go b/lib/httpserver/httpserver.go index 0ed106ac1e..cacdaeb587 100644 --- a/lib/httpserver/httpserver.go +++ b/lib/httpserver/httpserver.go @@ -23,7 +23,9 @@ import ( ) var ( - disableResponseCompression = flag.Bool("http.disableResponseCompression", false, "Disable compression of HTTP responses for saving CPU resources. By default compression is enabled to save network bandwidth") + disableResponseCompression = flag.Bool("http.disableResponseCompression", false, "Disable compression of HTTP responses for saving CPU resources. By default compression is enabled to save network bandwidth") + maxGracefulShutdownDuration = flag.Duration("http.maxGracefulShutdownDuration", 7*time.Second, "The maximum duration for graceful shutdown of HTTP server. "+ + "Highly loaded server may require increased value for graceful shutdown") servers = make(map[string]*http.Server) serversLock sync.Mutex @@ -103,10 +105,10 @@ func Stop(addr string) error { if s == nil { logger.Panicf("BUG: there is no http server at %q", addr) } - ctx, cancelFunc := context.WithTimeout(context.Background(), 5*time.Second) + ctx, cancelFunc := context.WithTimeout(context.Background(), *maxGracefulShutdownDuration) defer cancelFunc() if err := s.Shutdown(ctx); err != nil { - return fmt.Errorf("cannot gracefully shutdown http server at %q: %s", addr, err) + return fmt.Errorf("cannot gracefully shutdown http server at %q in %.3fs: %s", addr, maxGracefulShutdownDuration.Seconds(), err) } return nil }