File tree Expand file tree Collapse file tree 2 files changed +26
-23
lines changed
Expand file tree Collapse file tree 2 files changed +26
-23
lines changed Original file line number Diff line number Diff line change 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
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