Revisit waitgroup (#241)

* Fix Add to waitgroup out of goroutines calling wg.Done()
* Pass waitgroup to other loop functions
This commit is contained in:
Quentin McGaw
2020-09-12 14:34:15 -04:00
committed by GitHub
parent 1c012e4c92
commit e0e450ca1c
8 changed files with 33 additions and 20 deletions

View File

@@ -12,8 +12,8 @@ import (
)
type Looper interface {
Run(ctx context.Context)
RunRestartTicker(ctx context.Context)
Run(ctx context.Context, wg *sync.WaitGroup)
RunRestartTicker(ctx context.Context, wg *sync.WaitGroup)
Restart()
Stop()
GetPeriod() (period time.Duration)
@@ -74,7 +74,8 @@ func (l *looper) logAndWait(ctx context.Context, err error) {
<-ctx.Done()
}
func (l *looper) Run(ctx context.Context) {
func (l *looper) Run(ctx context.Context, wg *sync.WaitGroup) {
defer wg.Done()
select {
case <-l.restart:
case <-ctx.Done():
@@ -124,7 +125,8 @@ func (l *looper) Run(ctx context.Context) {
}
}
func (l *looper) RunRestartTicker(ctx context.Context) {
func (l *looper) RunRestartTicker(ctx context.Context, wg *sync.WaitGroup) {
defer wg.Done()
ticker := time.NewTicker(time.Hour)
period := l.GetPeriod()
if period > 0 {