-
Notifications
You must be signed in to change notification settings - Fork 306
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
On error callback #89
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,8 @@ type ErrorPool struct { | |
|
||
mu sync.Mutex | ||
errs error | ||
|
||
errorCallBack func(err error) | ||
} | ||
|
||
// Go submits a task to the pool. If all goroutines in the pool | ||
|
@@ -68,6 +70,13 @@ func (p *ErrorPool) WithMaxGoroutines(n int) *ErrorPool { | |
return p | ||
} | ||
|
||
// WithErrorCallback configures the pool to call f everytime a task returns error. | ||
func (p *ErrorPool) WithErrorCallback(f func(err error)) *ErrorPool { | ||
p.panicIfInitialized() | ||
p.errorCallBack = f | ||
return p | ||
} | ||
|
||
// deref is a helper that creates a shallow copy of the pool with the same | ||
// settings. We don't want to just dereference the pointer because that makes | ||
// the copylock lint angry. | ||
|
@@ -93,5 +102,9 @@ func (p *ErrorPool) addErr(err error) { | |
p.errs = errors.Append(p.errs, err) | ||
} | ||
p.mu.Unlock() | ||
|
||
if p.errorCallBack != nil { | ||
p.errorCallBack(err) | ||
} | ||
Comment on lines
+106
to
+108
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be behind the mutex? It seems useful to be able to guarantee that the callback will never be called concurrently. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have also thought about that but if the callback is doing something time intensive it would block the whole pool. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, makes sense. I also don't feel very strongly. I think it's fine the way it is, but we should document that the callback must be safe to call from multiple goroutines. |
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we expose an equivalent on
ResultErrorPool
andResultContextPool
?