Skip to content

net/http: allow modifying Server's base context #30694

Closed
@taralx

Description

@taralx

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:

  1. 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).
  2. 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.

cc: @bradfitz
cc: #18997

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions