Merge panics into errors in Parallel

This commit is contained in:
Jae Kwon
2018-03-25 00:04:47 +01:00
parent 87c0473730
commit e9cf47606c
2 changed files with 9 additions and 20 deletions

View File

@ -15,7 +15,6 @@ type Task func(i int) (val interface{}, err error, abort bool)
type TaskResult struct {
Value interface{}
Error error
Panic interface{}
}
type TaskResultCh <-chan TaskResult
@ -92,17 +91,6 @@ func (trs *TaskResultSet) FirstError() error {
return nil
}
// Returns the firstmost (by task index) panic as
// discovered by all previous Reap() calls.
func (trs *TaskResultSet) FirstPanic() interface{} {
for _, result := range trs.results {
if result.Panic != nil {
return result.Panic
}
}
return nil
}
//----------------------------------------
// Parallel
@ -128,7 +116,7 @@ func Parallel(tasks ...Task) (trs *TaskResultSet, ok bool) {
defer func() {
if pnk := recover(); pnk != nil {
atomic.AddInt32(numPanics, 1)
taskResultCh <- TaskResult{nil, nil, pnk}
taskResultCh <- TaskResult{nil, ErrorWrap(pnk, "Panic in task")}
taskDoneCh <- false
}
}()
@ -136,7 +124,7 @@ func Parallel(tasks ...Task) (trs *TaskResultSet, ok bool) {
var val, err, abort = task(i)
// Send val/err to taskResultCh.
// NOTE: Below this line, nothing must panic/
taskResultCh <- TaskResult{val, err, nil}
taskResultCh <- TaskResult{val, err}
// Decrement waitgroup.
taskDoneCh <- abort
}(i, task, taskResultCh)