From 30a99cd85b339bce498627582d604740385a3ba8 Mon Sep 17 00:00:00 2001 From: Camden Cheek Date: Sun, 12 Nov 2023 10:16:38 -0700 Subject: [PATCH] simplify collection of errors --- pool/error_pool.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/pool/error_pool.go b/pool/error_pool.go index 6e5aa99..2e999a5 100644 --- a/pool/error_pool.go +++ b/pool/error_pool.go @@ -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 @@ -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 @@ -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() } }