A story about Go http.Client
Or how I have learned to embrace http.RoundTripper
Using a http client in Go usually starts like this:
Everything works, until it doesn’t: a network blip, a connection reset, a slow response, etc.
After some research (I recommend reading The complete guide to Go net/http timeouts) you may end up writing something similar to:
Ok! But now the question is: how does http.Client really work?
Ok… what is the RoundTripper?
Way simpler than I would expect: for each request, it receives a response, if everything works.
The default transport.go implementation is interesting. However, what else could I use RoundTripper for?
Implementing a naïve Retry RoundTripper without any concurrency concerns or request copy:
The HTTP retry client continues retrying until it succeeds or the context timeout reached.
A very simple http client cache:
RoundTripper interface enables extending the HTTP behaviour in a composable way.
The more I learn about Go Standard Library, the more I acknowledge the attention and craft put into it.
