-
Notifications
You must be signed in to change notification settings - Fork 1
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
Using timeouts #8
Comments
Hey @DavidMet hope you are well also! While I am still learning about context myself, using context allows the user more control than just timeouts. You now have the ability to force cancel a job even before the timeout. I asked the author of workerpool for his feedback on this library and he explained to me why using context is the better route. I do agree it is a little more boilerplate but at the end of the day it's worth it. You can see his input in #7 As far as defer not working, in my tests it has been working so that may be a new bug. I am still cleaning things up internally and should have updated code pushed today. |
@DavidMet I just published v1.1.1 if you would like to take a look/test it out that would be awesome! |
It also looks like // Taken from your code example above
// ...
// this means the job will timeout in 1ms
timeout := time.Duration(time.Millisecond)
// ... |
I also added this test so we can check for this type of thing. workerpoolxt/workerpoolxt_test.go Line 607 in 261e230
|
Sorry meant to reference #9 instead |
Also, we do create a child context from the context you've provided. You should be able to just exclude the cancel func if you don't need to call it. |
@oze4 - well, I understand the situation where it's valuable to add the context, my question now, do you think there is a way to simplify the usage of it , timeout with context ...,if so, could you please update the docs.. |
@DavidMet you could write a func that returns a new context with timeout based upon a parameter. Then call that func in each jobs Context. This is not the responsibility of this library to enforce or document how people create context. For example, in my tests I am using a func to create the context. You can do the something like this but use a parameter to pass the timeout to context.WithTimeout. workerpoolxt/workerpoolxt_test.go Line 24 in 80320f8
workerpoolxt/workerpoolxt_test.go Line 38 in 80320f8
|
@DavidMet I was finally able to get to my computer... Something like this is what I mean... func newContextWithTimeout(timeout time.Duration) context.Context {
r, _ := context.WithTimeout(context.Background(), timeout)
return r
}
func main() {
defaultContext := context.Background()
wp := wpxt.New(defaultContext, 10)
wp.SubmitXT(wpxt.Job{
Name: "a",
// Set timeout to whatever for each job....
Context: newContextWithTimeout(time.Duration(time.Second*100)),
Task: // ...
})
wp.SubmitXT(wpxt.Job{
Name: "a",
Context: newContextWithTimeout(time.Duration(time.Millisecond*5)),
Task: // ...
})
// ...
} |
yes. what about the
|
Hi @oze4 , Hope you doing good!
I've tried the latest version and I saw that you made some changes to the timeout and add the contexts,
3.Btw, when you use the
defer done()
the job doesnt wait to the timeout, it finish immeditally, should I use it like this? or I can remove thedefer
and use it likemyCtx, _ := context.WithTimeout(context.Background(), timeout)
?Thanks a lot!
The text was updated successfully, but these errors were encountered: