diff --git a/internal/server/server.go b/internal/server/server.go index fe71103..98d482b 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -8,7 +8,6 @@ import ( "net" "net/http" "os" - "sync" "time" "golang.org/x/crypto/acme" @@ -58,28 +57,12 @@ func (s *Server) Stop() { ctx, cancel := context.WithTimeout(context.Background(), shutdownTimeout) defer cancel() - s.commandHandler.Close() - - var wg sync.WaitGroup - wg.Add(2) - go func() { - err := s.httpServer.Shutdown(ctx) - if err != nil { - slog.Warn("Error while stopping http server", "error", err) - } - slog.Debug("Server http stopped") - wg.Done() - }() - go func() { - err := s.httpsServer.Shutdown(ctx) - if err != nil { - slog.Warn("Error while stopping https server", "error", err) - } - slog.Debug("Server https stopped") - wg.Done() - }() - - wg.Wait() + PerformConcurrently( + func() { _ = s.commandHandler.Close() }, + func() { _ = s.httpServer.Shutdown(ctx) }, + func() { _ = s.httpsServer.Shutdown(ctx) }, + ) + slog.Info("Server stopped") } diff --git a/internal/server/util.go b/internal/server/util.go new file mode 100644 index 0000000..4000e06 --- /dev/null +++ b/internal/server/util.go @@ -0,0 +1,20 @@ +package server + +import ( + "sync" +) + +func PerformConcurrently(fns ...func()) { + var wg sync.WaitGroup + + wg.Add(len(fns)) + + for _, fn := range fns { + go func() { + defer wg.Done() + fn() + }() + } + + wg.Wait() +}