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 return nil
} }
// final closes indexCh after all workers have terminated
final := func() {
close(resultCh)
}
// run workers on ch // run workers on ch
wg.Go(func() error { wg.Go(func() error {
return repository.RunWorkers(defaultParallelism, worker, final) defer close(resultCh)
return repository.RunWorkers(defaultParallelism, worker)
}) })
// receive decoded indexes // receive decoded indexes

View file

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

View file

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