Using a waitgroup to wait for all programs to exit

This commit is contained in:
Quentin McGaw
2020-07-08 23:36:02 +00:00
parent 8669748289
commit 99e386abc8
6 changed files with 33 additions and 36 deletions

View File

@@ -4,13 +4,14 @@ import (
"context"
"fmt"
"net/http"
"sync"
"time"
"github.com/qdm12/golibs/logging"
)
type Server interface {
Run(ctx context.Context, serverDone chan struct{})
Run(ctx context.Context, wg *sync.WaitGroup)
}
type server struct {
@@ -29,10 +30,11 @@ func New(address string, logger logging.Logger, restartOpenvpn, restartUnbound c
}
}
func (s *server) Run(ctx context.Context, serverDone chan struct{}) {
func (s *server) Run(ctx context.Context, wg *sync.WaitGroup) {
wg.Add(1)
server := http.Server{Addr: s.address, Handler: s.makeHandler()}
go func() {
defer close(serverDone)
defer wg.Done()
<-ctx.Done()
s.logger.Warn("context canceled: exiting loop")
shutdownCtx, cancel := context.WithTimeout(context.Background(), 2*time.Second)