repository: Remove RunWorkers, report ctx.Err()

This removes RunWorkers, which had become mere overhead by successive
refactors. It also ensures that each former user of that function
returns any context error that occurs, so failure to complete an
operation is always reported as an error.
This commit is contained in:
greatroar 2022-05-10 22:17:50 +02:00
parent ab49c14621
commit 2e0f1f5113
4 changed files with 12 additions and 31 deletions

View File

@ -37,7 +37,7 @@ func ForAllIndexes(ctx context.Context, repo restic.Repository,
return repo.List(ctx, restic.IndexFile, func(id restic.ID, size int64) error {
select {
case <-ctx.Done():
return nil
return ctx.Err()
case ch <- FileInfo{id, size}:
}
return nil
@ -69,9 +69,9 @@ func ForAllIndexes(ctx context.Context, repo restic.Repository,
}
// run workers on ch
wg.Go(func() error {
return RunWorkers(loadIndexParallelism, worker)
})
for i := 0; i < loadIndexParallelism; i++ {
wg.Go(worker)
}
return wg.Wait()
}

View File

@ -365,7 +365,7 @@ func (mi *MasterIndex) Save(ctx context.Context, repo restic.Repository, packBla
select {
case ch <- newIndex:
case <-ctx.Done():
return nil
return ctx.Err()
}
newIndex = NewIndex()
}
@ -397,10 +397,9 @@ func (mi *MasterIndex) Save(ctx context.Context, repo restic.Repository, packBla
}
// run workers on ch
wg.Go(func() error {
return RunWorkers(saveIndexParallelism, worker)
})
for i := 0; i < saveIndexParallelism; i++ {
wg.Go(worker)
}
err = wg.Wait()
return obsolete, err

View File

@ -679,7 +679,7 @@ func (r *Repository) CreateIndexFromPacks(ctx context.Context, packsize map[rest
for id, size := range packsize {
select {
case <-ctx.Done():
return nil
return ctx.Err()
case ch <- FileInfo{id, size}:
}
}
@ -705,9 +705,9 @@ func (r *Repository) CreateIndexFromPacks(ctx context.Context, packsize map[rest
}
// run workers on ch
wg.Go(func() error {
return RunWorkers(listPackParallelism, worker)
})
for i := 0; i < listPackParallelism; i++ {
wg.Go(worker)
}
err = wg.Wait()
if err != nil {

View File

@ -1,18 +0,0 @@
package repository
import (
"golang.org/x/sync/errgroup"
)
// RunWorkers runs count instances of workerFunc using an errgroup.Group.
// 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
for i := 0; i < count; i++ {
wg.Go(workerFunc)
}
return wg.Wait()
}