defer close(ch) outside repository.RunWorkers

This commit is contained in:
greatroar 2020-10-14 14:23:06 +02:00
parent 6003dada14
commit b27375f5ce
3 changed files with 7 additions and 24 deletions

View file

@ -135,14 +135,10 @@ func (c *Checker) LoadIndex(ctx context.Context) (hints []error, errs []error) {
return nil
}
// final closes indexCh after all workers have terminated
final := func() {
close(resultCh)
}
// run workers on ch
wg.Go(func() error {
return repository.RunWorkers(defaultParallelism, worker, final)
defer close(resultCh)
return repository.RunWorkers(defaultParallelism, worker)
})
// receive decoded indexes

View file

@ -476,14 +476,10 @@ func (r *Repository) LoadIndex(ctx context.Context) error {
return nil
}
// final closes indexCh after all workers have terminated
final := func() {
close(indexCh)
}
// run workers on ch
wg.Go(func() error {
return RunWorkers(loadIndexParallelism, worker, final)
defer close(indexCh)
return RunWorkers(loadIndexParallelism, worker)
})
// receive decoded indexes

View file

@ -5,10 +5,8 @@ import (
)
// RunWorkers runs count instances of workerFunc using an errgroup.Group.
// After all workers have terminated, finalFunc is run. If an error occurs in
// one of the workers, it is returned. FinalFunc is always run, regardless of
// any other previous errors.
func RunWorkers(count int, workerFunc func() error, finalFunc func()) error {
// If an error occurs in one of the workers, it is returned.
func RunWorkers(count int, workerFunc func() error) error {
var wg errgroup.Group
// run workers
@ -16,12 +14,5 @@ func RunWorkers(count int, workerFunc func() error, finalFunc func()) error {
wg.Go(workerFunc)
}
// wait for termination
err := wg.Wait()
// make sure finalFunc is run
finalFunc()
// return error from workers to the caller
return err
return wg.Wait()
}