Skip to content

Commit

Permalink
Warn when server shutdown closes active connections
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinmcconnell committed Jan 6, 2025
1 parent 10cec1d commit eb87755
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package server
import (
"context"
"crypto/tls"
"errors"
"fmt"
"log/slog"
"net"
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)
}
}
}

0 comments on commit eb87755

Please sign in to comment.