Skip to content

proposal: context: functional interfaces #62017

@eccles

Description

@eccles

Description

The context.Context fulfils 3 different purposes:

  • bag of values
  • deadline
  • cancellation.

These different functionalities are not apparent in any API - one has to rely on documentation or read the source code to find out which of the different functionalities is expeceted from the user of any specific API.

I proppose that we take a leaf out of the io.Reader, io.ReadWriter ... interfaces in the io package and define:

  • context.Canceller
  • context.Values
  • context.Deadline
  • context.DeadLineCanceller
  • context.ValuesCanceller
  • context.ValuesDeadline
  • context.Context - all of them for backwards compatibility

API authors can then specify the correct context and this will document in the code the expected context behaviour.

Cons:

  • Some packages will parse the context and act accordingly and this scheme will not work for them without some modification. For example the azure-sdk-for-go will enact a Timeout if the passed in context has a Timeout attribute - otherwise it reverts to an internal retry with backoff.
  • adopting this for any API will probably cause a major version change.

From experience

Coding guidelines can dictate that a context contain values for general use such as headers or grpc metadayta. This leads to the decision to mandate that every network call uses a common context without realising that this common context might also contain a deadline which on every call becomes shorter and shorter leading to general flakiness. Fixing this in a large codebase without knowing what is expected of any passed in context is not trivial perhaps.

Hence the recent introduction of context.WithoutCancel().

I would assert that if we had the above subcontexts then WithoutCaancel may not be required.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Incoming

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions