From 61b646001427391a7cf42408253d68bc30cdb74d Mon Sep 17 00:00:00 2001 From: jholdstock Date: Sat, 16 Sep 2023 07:45:06 +0100 Subject: [PATCH] webapi: Use existing context for server shutdown. The server will shutdown cleanly even if the passed context is already canceled, so there is no need to pass a new context and blindly guess at how long the server may need to stop. --- internal/webapi/webapi.go | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/internal/webapi/webapi.go b/internal/webapi/webapi.go index 941b091..b7cd3ec 100644 --- a/internal/webapi/webapi.go +++ b/internal/webapi/webapi.go @@ -148,14 +148,11 @@ func (w *WebAPI) Run(ctx context.Context) { // Add the graceful shutdown to the waitgroup. wg.Add(1) go func() { - // Wait until shutdown is signaled before shutting down. + // Wait until context is canceled before shutting down the server. <-ctx.Done() w.log.Debug("Stopping webserver...") - // Give the webserver 10 seconds to finish what it is doing. - timeoutCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second) - defer cancel() - if err := w.server.Shutdown(timeoutCtx); err != nil { + if err := w.server.Shutdown(ctx); err != nil { w.log.Errorf("Failed to stop webserver cleanly: %v", err) } else { w.log.Debug("Webserver stopped")