-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
proposal: context: functional interfaces #62017
Comments
Hi @eccles, The For example, in my own application I might have decided to use a particular distributed tracing instrumentation API, but it's unlikely that third-party libraries I'm using will have made the same decision. If I'm using a function in that third-party library to indirectly call a function in my own application -- a typical example being stdlib With that said, I feel unsure about how the more specific interfaces you described here would be used. You say "API authors can then specify the correct context", but you've not described how the API author would decide which type to use. If you are imagining using this only for calls between functions in a single codebase then indeed for that case I could imagine how that might work, but that situation is not the scenario that the Of course that doesn't mean that the scope of |
CC @Sajmani |
Thank you @apparentlymart for your comments. I am no proposing that we get rid of the generic context.Context. Just simply making it clearer for any particular API on how that AP is expected to be used with regards to the required context.Context argument. So for example calling a listener of some kind (httpserver or similar):
which indicates that this function could be cancelled or
which indicates that a timeout should be specified in the context and the Values will be used (maybe in the headers...) or
which indicates that only a deadline will be expected in the context. |
Just for emphasis this proposal simply aims to make the intent of the API of any package clearer. |
The advantage of context.Context is that we have a single type used across the ecosystem to carry cross-cutting concerns across API boundaries. There are various unfortunate aspects to how Contexts are defined, but they are still widely used because a single, common type outweighs the negatives. Splitting Context into a variety of types would undo that advantage. |
@neild Thanks for your comment but my experience in trying to figure out how a generic context argument is intended to be used and having to resort to reading the many layers of code involved in the third party package contradicts. I see context.context as some form of God object whose intended use is unclear. We have precedent in the way io.Reader and its related interfaces are constructed and this seems to be a good pattern. |
Description
The context.Context fulfils 3 different purposes:
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:
API authors can then specify the correct context and this will document in the code the expected context behaviour.
Cons:
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.
The text was updated successfully, but these errors were encountered: