Handle errors and cancel process when encountered

This commit is contained in:
Dane Everitt 2020-07-16 22:18:47 -07:00
parent a72d6f3768
commit daf682b991
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53

View File

@ -1,6 +1,7 @@
package server package server
import ( import (
"context"
"github.com/gammazero/workerpool" "github.com/gammazero/workerpool"
"io/ioutil" "io/ioutil"
"os" "os"
@ -17,6 +18,10 @@ type PooledFileWalker struct {
wg sync.WaitGroup wg sync.WaitGroup
pool *workerpool.WorkerPool pool *workerpool.WorkerPool
callback filepath.WalkFunc callback filepath.WalkFunc
cancel context.CancelFunc
err error
errOnce sync.Once
Filesystem *Filesystem Filesystem *Filesystem
} }
@ -85,7 +90,14 @@ func (w *PooledFileWalker) push(path string) {
w.wg.Add(1) w.wg.Add(1)
w.pool.Submit(func() { w.pool.Submit(func() {
defer w.wg.Done() defer w.wg.Done()
w.process(path) if err := w.process(path); err != nil {
w.errOnce.Do(func() {
w.err = err
if w.cancel != nil {
w.cancel()
}
})
}
}) })
} }
@ -95,10 +107,17 @@ func (fs *Filesystem) Walk(dir string, callback filepath.WalkFunc) error {
w := newPooledWalker(fs) w := newPooledWalker(fs)
w.callback = callback w.callback = callback
_, cancel := context.WithCancel(context.Background())
w.cancel = cancel
w.push(dir) w.push(dir)
w.wg.Wait() w.wg.Wait()
w.pool.StopWait() w.pool.StopWait()
if w.err != nil {
return w.err
}
return nil return nil
} }