WIP types: use context.Context for public API #43
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This patch adds a
context.Context
argument to every public function we export in our public types.Why?
We provide a library which makes http calls to remote services which can timeout indefinitely or more than how long the user of our library is willing to wait (or is expected to).
There's an important Go concurrency pattern which is in use in the http.Request object. Specifically the Request object provides a Cancel struct which is a channel that whenever is closed the request/response is closed accordingly. So, a user can provide his own context and do his stuff in the caller and if needed close the underlying http resource at his will w/o leaking anything.
As a plus, context.Context is being moved to the standard library in Go 1.7 and it's already widely accepted to have it as the first arg of public functions (see Docker, Protobuf, Docker/distribution.. and many more)
More info here:
https://blog.golang.org/context
https://blog.cloudflare.com/the-complete-guide-to-golang-net-http-timeouts/
Signed-off-by: Antonio Murdaca runcom@redhat.com