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

@@ -2,6 +2,7 @@ package tinyproxy
import (
"context"
"sync"
"time"
"github.com/qdm12/golibs/command"
@@ -12,7 +13,7 @@ import (
)
type Looper interface {
Run(ctx context.Context, restart <-chan struct{}, done chan<- struct{})
Run(ctx context.Context, restart <-chan struct{}, wg *sync.WaitGroup)
}
type looper struct {
@@ -44,11 +45,12 @@ func NewLooper(conf Configurator, firewallConf firewall.Configurator, settings s
}
}
func (l *looper) Run(ctx context.Context, restart <-chan struct{}, done chan<- struct{}) {
func (l *looper) Run(ctx context.Context, restart <-chan struct{}, wg *sync.WaitGroup) {
wg.Add(1)
defer wg.Done()
select {
case <-restart:
case <-ctx.Done():
close(done)
return
}
for {
@@ -89,7 +91,6 @@ func (l *looper) Run(ctx context.Context, restart <-chan struct{}, done chan<- s
l.logger.Warn("context canceled: exiting loop")
tinyproxyCancel()
close(waitError)
close(done)
return
case <-restart: // triggered restart
l.logger.Info("restarting")