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.
This commit is contained in:
jholdstock 2023-09-16 07:45:06 +01:00 committed by Jamie Holdstock
parent f881179024
commit 61b6460014

View File

@ -148,14 +148,11 @@ func (w *WebAPI) Run(ctx context.Context) {
// Add the graceful shutdown to the waitgroup. // Add the graceful shutdown to the waitgroup.
wg.Add(1) wg.Add(1)
go func() { go func() {
// Wait until shutdown is signaled before shutting down. // Wait until context is canceled before shutting down the server.
<-ctx.Done() <-ctx.Done()
w.log.Debug("Stopping webserver...") w.log.Debug("Stopping webserver...")
// Give the webserver 10 seconds to finish what it is doing. if err := w.server.Shutdown(ctx); err != nil {
timeoutCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := w.server.Shutdown(timeoutCtx); err != nil {
w.log.Errorf("Failed to stop webserver cleanly: %v", err) w.log.Errorf("Failed to stop webserver cleanly: %v", err)
} else { } else {
w.log.Debug("Webserver stopped") w.log.Debug("Webserver stopped")