From b27375f5ce5c5ca846aab0830ae8f884ac728a82 Mon Sep 17 00:00:00 2001 From: greatroar <@> Date: Wed, 14 Oct 2020 14:23:06 +0200 Subject: [PATCH] defer close(ch) outside repository.RunWorkers --- internal/checker/checker.go | 8 ++------ internal/repository/repository.go | 8 ++------ internal/repository/worker_group.go | 15 +++------------ 3 files changed, 7 insertions(+), 24 deletions(-) diff --git a/internal/checker/checker.go b/internal/checker/checker.go index 1f3233a01..8e553d9bf 100644 --- a/internal/checker/checker.go +++ b/internal/checker/checker.go @@ -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 diff --git a/internal/repository/repository.go b/internal/repository/repository.go index 194d1e30c..b2ffb9e37 100644 --- a/internal/repository/repository.go +++ b/internal/repository/repository.go @@ -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 diff --git a/internal/repository/worker_group.go b/internal/repository/worker_group.go index a87ef4f49..c612d1d22 100644 --- a/internal/repository/worker_group.go +++ b/internal/repository/worker_group.go @@ -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() }