-
Notifications
You must be signed in to change notification settings - Fork 18.7k
Closed
Labels
Milestone
Description
@Sajmani and I are interested in adding golang.org/x/net/context to the standard library, probably as the top-level package context.
(Why not net/context? It has little to do with networking, except that people often use it with networking; it doesn't even cross network boundaries on its own without help.)
Some places in the standard library which could use it:
- https://golang.org/pkg/net/http/#Request -- add a new
Contextfield, alongside like the existing (Context.Done-compatible)Request.Cancelfield. It will take precedence overCancel, and we can make it work for both outbound and inbound requests. For server requests it would useCloseNotifier's mechanism for c* ancelation. (which for http2 includes the http2-level explicit stream cancelation frame too, also used by gRPC) - https://golang.org/pkg/net/#Dialer -- same as
http.Request.net.Dialeralso has a Done-compatibleCancelfield, which could be a Context. - https://golang.org/pkg/os/exec/#Cmd could have a new
Contextfield to automatically kill+wait the process when a context expires, if the context expires before the child? - https://golang.org/pkg/database/sql/#DB.Query already takes
...interface{}which can be special-cased to say that if the first parameter is acontext.Context, it's used for deadlines. Trashy or clever? Maybe both. We could also add parallelDB.QueryContextwhich is a bit of a mouthful. - .... (where else?) ....
Open questions:
- do we do something with the
iopackage in the same release, or later? i.e. do we add*os.File.IO(context.Context) io.ReadWriteClosersort of methods, to curry a context intoiointerfaces? Or wait on that. If it'd be nice to push down cancelation into even OS-level file I/O, but we don't even really do that inside Google withIO-like methods and pervasive Context usage, since we don't really use*os.File. So I'm inclined to wait on that. Too many operating systems to deal with.
Concerns:
- we can't add it to as many places in the standard library we'd like to since APIs are frozen. Basically we can only add it to structs. While we've told people not to add contexts to structs, I think that guidance is over-aggressive. The real advice is not to store contexts. They should be passed along like parameters. But if the struct is essentially just a parameter, it's okay. I think this concern can be addressed with package-level documentation and examples.
- what field name in structs to use? We use "ctx" for variables, but struct names are typically spelled out. But does that cause too much stutter? e.g.
package http
type Request struct {
Context context.Context
}stuartnelson3, djui, Thomasdezeeuw, andrewhampton, tbroyer and 74 more