From eb87755ec01396564410fb6f08541a6a00f832a8 Mon Sep 17 00:00:00 2001 From: Kevin McConnell Date: Mon, 6 Jan 2025 11:26:21 +0000 Subject: [PATCH] Warn when server shutdown closes active connections --- internal/server/server.go | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/internal/server/server.go b/internal/server/server.go index 98d482b..f06613f 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -3,6 +3,7 @@ package server import ( "context" "crypto/tls" + "errors" "fmt" "log/slog" "net" @@ -59,8 +60,8 @@ func (s *Server) Stop() { PerformConcurrently( func() { _ = s.commandHandler.Close() }, - func() { _ = s.httpServer.Shutdown(ctx) }, - func() { _ = s.httpsServer.Shutdown(ctx) }, + func() { s.stopHTTPServer(ctx, s.httpServer) }, + func() { s.stopHTTPServer(ctx, s.httpsServer) }, ) slog.Info("Server stopped") @@ -131,3 +132,14 @@ func (s *Server) buildHandler() http.Handler { return handler } + +func (s *Server) stopHTTPServer(ctx context.Context, server *http.Server) { + err := server.Shutdown(ctx) + if err != nil { + if errors.Is(err, context.DeadlineExceeded) { + slog.Warn("Closing active connections") + } else { + slog.Error("Error while attempting to stop server", "error", err) + } + } +}