Skip to content

Commit

Permalink
Extact a helper to run shutdowns concurrently
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinmcconnell committed Jan 3, 2025
1 parent 6f51bd7 commit 10cec1d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 23 deletions.
29 changes: 6 additions & 23 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"net"
"net/http"
"os"
"sync"
"time"

"golang.org/x/crypto/acme"
Expand Down Expand Up @@ -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")
}

Expand Down
20 changes: 20 additions & 0 deletions internal/server/util.go
Original file line number Diff line number Diff line change
@@ -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()
}

0 comments on commit 10cec1d

Please sign in to comment.