-
Notifications
You must be signed in to change notification settings - Fork 139
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
Add panic-safe TrySubmit and TrySubmitWait with backward compatibility. #77
base: main
Are you sure you want to change the base?
Conversation
func (p *WorkerPool) TrySubmit(task func()) error { | ||
if p.Stopped() { | ||
return ErrWorkerStopped | ||
} |
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.
Seems there is a race here: If WorkerPool is stopped after the p.Stopped
check, but before the call to p.Submit
. Consider using recover
and checking for a write to a closed channel error, and then returning ErrWorkerStopped
.
I was thinking of something like this: https://github.com/gammazero/workerpool/pull/80/files, but... Really, I want If a caller needs to handle the panic and return an error or ignore it, then they can easily wrap the channel send or the call to |
select { | ||
case <-doneCh: | ||
break | ||
case <-ctx.Done(): |
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.
case <-ctx.Done(): | |
case <- time.After(100 * time.Millisecond): |
Simpler to not create a context for the timeout.
This PR introduces new methods TrySubmit and TrySubmitWait to provide a panic-safe way to submit tasks to the worker pool, providing clear and explicit way for users to handle the stopped state of the worker pool without breaking backward compatibility by changing existing behavior in a non-obvious for users way.
Note: