Skip to content

Commit 10cec1d

Browse files
Extact a helper to run shutdowns concurrently
1 parent 6f51bd7 commit 10cec1d

File tree

2 files changed

+26
-23
lines changed

2 files changed

+26
-23
lines changed

internal/server/server.go

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"net"
99
"net/http"
1010
"os"
11-
"sync"
1211
"time"
1312

1413
"golang.org/x/crypto/acme"
@@ -58,28 +57,12 @@ func (s *Server) Stop() {
5857
ctx, cancel := context.WithTimeout(context.Background(), shutdownTimeout)
5958
defer cancel()
6059

61-
s.commandHandler.Close()
62-
63-
var wg sync.WaitGroup
64-
wg.Add(2)
65-
go func() {
66-
err := s.httpServer.Shutdown(ctx)
67-
if err != nil {
68-
slog.Warn("Error while stopping http server", "error", err)
69-
}
70-
slog.Debug("Server http stopped")
71-
wg.Done()
72-
}()
73-
go func() {
74-
err := s.httpsServer.Shutdown(ctx)
75-
if err != nil {
76-
slog.Warn("Error while stopping https server", "error", err)
77-
}
78-
slog.Debug("Server https stopped")
79-
wg.Done()
80-
}()
81-
82-
wg.Wait()
60+
PerformConcurrently(
61+
func() { _ = s.commandHandler.Close() },
62+
func() { _ = s.httpServer.Shutdown(ctx) },
63+
func() { _ = s.httpsServer.Shutdown(ctx) },
64+
)
65+
8366
slog.Info("Server stopped")
8467
}
8568

internal/server/util.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package server
2+
3+
import (
4+
"sync"
5+
)
6+
7+
func PerformConcurrently(fns ...func()) {
8+
var wg sync.WaitGroup
9+
10+
wg.Add(len(fns))
11+
12+
for _, fn := range fns {
13+
go func() {
14+
defer wg.Done()
15+
fn()
16+
}()
17+
}
18+
19+
wg.Wait()
20+
}

0 commit comments

Comments
 (0)