Closed
Description
This is #16220 revived, as that one is locked due to age.
Contexts are everywhere now, and can carry significant information, like authentication data. It is therefore useful to us to be able to set the base context to something other than context.Background()
.
(#20956 has a similar request, although that one is context modification per-connection instead of per-listener.)
While it is possible to do this by wrapping the handler and merging the contexts, this is error-prone and requires an additional goroutine to properly merge the Done channels.
The main question is one of API. There are two options that I can think of:
- Add a field BaseContext to the http.Server struct.
Advantages: Keeps the API the same, easy to retrofit, composable with existing systems that take/manipulate http.Server.
Disadvantages: We discourage storing contexts in structs because it makes it easier to misuse them (e.g. using a stale context). - Add a Context parameter to the Serve call.
Advantages: Standard way to provide base context; difficult to misuse.
Disadvantages: API proliferation -- we need ServeContext for each variant of Serve.