File tree Expand file tree Collapse file tree 2 files changed +37
-2
lines changed
Expand file tree Collapse file tree 2 files changed +37
-2
lines changed Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ package server
33import (
44 "context"
55 "crypto/tls"
6+ "errors"
67 "fmt"
78 "log/slog"
89 "net"
@@ -57,8 +58,11 @@ func (s *Server) Stop() {
5758 ctx , cancel := context .WithTimeout (context .Background (), shutdownTimeout )
5859 defer cancel ()
5960
60- s .commandHandler .Close ()
61- s .httpServer .Shutdown (ctx )
61+ PerformConcurrently (
62+ func () { _ = s .commandHandler .Close () },
63+ func () { s .stopHTTPServer (ctx , s .httpServer ) },
64+ func () { s .stopHTTPServer (ctx , s .httpsServer ) },
65+ )
6266
6367 slog .Info ("Server stopped" )
6468}
@@ -128,3 +132,14 @@ func (s *Server) buildHandler() http.Handler {
128132
129133 return handler
130134}
135+
136+ func (s * Server ) stopHTTPServer (ctx context.Context , server * http.Server ) {
137+ err := server .Shutdown (ctx )
138+ if err != nil {
139+ if errors .Is (err , context .DeadlineExceeded ) {
140+ slog .Warn ("Closing active connections" )
141+ } else {
142+ slog .Error ("Error while attempting to stop server" , "error" , err )
143+ }
144+ }
145+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments