Skip to content

Commit

Permalink
Improve readablility
Browse files Browse the repository at this point in the history
  • Loading branch information
Corentin Clabaut committed Jun 17, 2024
1 parent 57e5a0a commit f681bc1
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ func worker(context context.Context, waitGroup *sync.WaitGroup, firstTask func()
select {
case <-context.Done():
// Pool context was cancelled, empty tasks channel and exit
for _ = range tasks {
taskWaitGroup.Done()
}
drainTasks(tasks, taskWaitGroup)
return
case task, ok := <-tasks:
// Prioritize context.Done statement (https://stackoverflow.com/questions/46200343/force-priority-of-go-select-statement)
Expand All @@ -45,3 +43,10 @@ func worker(context context.Context, waitGroup *sync.WaitGroup, firstTask func()
}
}
}

// drainPendingTasks discards queued tasks and decrements the corresponding wait group
func drainTasks(tasks <-chan func(), tasksWaitGroup *sync.WaitGroup) {
for _ = range tasks {
tasksWaitGroup.Done()
}
}

0 comments on commit f681bc1

Please sign in to comment.