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
proposal: Add time.AfterContext #40736
Comments
Have you considered: ctx, cancel := context.WithTimeout(ctx, time.Second*5)
defer cancel()
<-ctx.Done()
if ctx.Err() != nil {
return
} |
While that's certainly shorter, in your example you're shadowing the parent ctx which you'll want to use afterwards. You could use two different contexts but it's easy to get confused between them. I think the proposed function fits nicely in the It'd help this proposal a lot if I had some objective data on how often this comes up in practice but I don't and I'm not really sure how I would go about doing so. |
Note the proposed API would be impossible due to the import cycle between Also don't think this fits into the |
It seems to me that this can be written outside of the standard library. https://golang.org/doc/faq#x_in_std |
Very true.
Not sure what you mean by this, it is dealing directly with time? Anyhow, will close as like you mentioned, the proposed API isn't going to work. We could get around the import cycle with some bank but I don't think that's justified for a single function. |
Often times I've found myself wanting to have a delay in my program that is interruptible.
Presently I have to write such code as:
This is verbose and easy to get wrong if you use
time.After
instead oftime.NewTimer
in an attempt to make it shorter as you'll end up leaking timers when the context is cancelled.I propose we add the following function to the time package to make the above use case easier and safer.
I couldn't come up with a good use case or API for
AfterFunc
with context interruption so I left it out.Example
Caveat
The name is certainly confusing. Please comment if you have a better suggestion.
The text was updated successfully, but these errors were encountered: