Skip to content

Commit

Permalink
simplify collection of errors
Browse files Browse the repository at this point in the history
  • Loading branch information
camdencheek committed Nov 12, 2023
1 parent a68c69f commit 30a99cd
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions pool/error_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type ErrorPool struct {
onlyFirstError bool

mu sync.Mutex
errs error
errs []error
}

// Go submits a task to the pool. If all goroutines in the pool
Expand All @@ -35,7 +35,13 @@ func (p *ErrorPool) Go(f func() error) {
// returning any errors from tasks.
func (p *ErrorPool) Wait() error {
p.pool.Wait()
return p.errs
if len(p.errs) == 0 {
return nil
} else if p.onlyFirstError {
return p.errs[0]
} else {
return multierror.Join(p.errs...)
}
}

// WithContext converts the pool to a ContextPool for tasks that should
Expand Down Expand Up @@ -85,13 +91,7 @@ func (p *ErrorPool) panicIfInitialized() {
func (p *ErrorPool) addErr(err error) {
if err != nil {
p.mu.Lock()
if p.onlyFirstError {
if p.errs == nil {
p.errs = err
}
} else {
p.errs = multierror.Join(p.errs, err)
}
p.errs = append(p.errs, err)
p.mu.Unlock()
}
}

0 comments on commit 30a99cd

Please sign in to comment.