-
Notifications
You must be signed in to change notification settings - Fork 18k
proposal: builtin: First-Class Cancellation #10562
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
Comments
I don't see any proposed language changes. Do you mean it should be part of the Go standard library? Otherwise, it just seems to be an extension to the golang.org/x/net/context API, namely adding the And and Or functions. Maybe you can elaborate on why those are useful. |
The interface "error" is part of the language spec, not just the standard library. E.g. it doesn't have a package and is documented in the pseudo-package "builtin". The interface cancel should have the same treatment. I agree there is considerable overlap between cancellation and network contexts. Networks contexts are, however, a proper superset of cancellation. They add deadlines and timeouts which require a notion of time that is not universal. They add "values" which provide the additional context of an RPC or networking call. (The latter being where the name "context" comes from.) These additions, while important in their domain, are not required elements of the most basic cancellation framework and should be stripped from the core language feature. It is certainly reasonable that net/context.Context would "derive from" the cancel interface. Since cancel is a proper subset of its definition any context.Context would, of course, already implement cancel even if the definition were not changed. This provides for a convenient backward compatibility in the deployment of the new language feature. |
Please discuss language change proposals on the golang-nuts mailing list
first. Thanks.
|
Closing until there's an agreement to do something. Like Minux alluded, Go uses mailing lists for discussion unlike many Github projects which converse in Issues. Either model is fine, but historically we've had and used mailing lists. We can reopen this if/when the time comes. |
Can someone add a link to the relevant mailing list thread? I came here from Google looking for cancellation token support. |
Go should have a first-class cancellation pattern (in much the same way that it has a first class error pattern). The cancellation pattern should meet the following requirements:
observe
cancellation and the ability tocause
cancellation MUST be independent.observe
the same cancellation token without impacting each other.observe
cancellation in parallel without additional synchronization.poll
whether cancellation has occurred. This allows long running computations to periodically check whether they should stop running even if they never block on IO.select
statement. e.g. block on a data channel value or cancellation, which ever happens first.and
oror
manner such that you get a new cancellation token that becomes cancelled when its inputs become cancelled.diagnostic
message that can be used in logging or other debugging to indicate the reason a computation was asked to cancel.What Is It For
Cancellation allows a tree or pipeline of computation whose output is no longer needed to be terminated. Go makes describing pipelines of computation easy. Goroutines representing each stage of such a pipeline link together via channels to form a computational pipeline whose final channel delivers the final result of the computation. Once a pipeline has produced sufficient output for the final consumer the pipeline needs to be torn down. Sometimes this corresponds to natural boundaries of data being produced but sometimes it doesn't (e.g. when errors are encountered or the consumer disconnects abruptly).
What
I propose that cancellation be implemented like errors through an interface of the following form:
This interface allows for implementations that meet all of the above specifications. Because the Done() channel's write endpoint is closely held in this design, it is possible for a language runtime implementation to use a cheaper channel implementation that allows for Boolean and Hierarchical composition without actually allocating a goroutine to wait on the inputs.
Why It Should Be Part Of The Language
For most of the same reasons that error is part of the language. First-class treatment of cancellation will guarantee the proper level of composability that an external implementation cannot achieve.
First-class treatment allows the core libraries to also level the pattern.
As mentioned above, a language runtime level implementation can be significantly less expensive for composition that would be possible by a pure library implementation. A low-cost enables its ubiquity as it allows it to be used in domains that simply would not permit a more expensive implementation.
The text was updated successfully, but these errors were encountered: