]>
Commit | Line | Data |
---|---|---|
c680a8e1 RS |
1 | # cleanhttp |
2 | ||
3 | Functions for accessing "clean" Go http.Client values | |
4 | ||
5 | ------------- | |
6 | ||
7 | The Go standard library contains a default `http.Client` called | |
8 | `http.DefaultClient`. It is a common idiom in Go code to start with | |
9 | `http.DefaultClient` and tweak it as necessary, and in fact, this is | |
10 | encouraged; from the `http` package documentation: | |
11 | ||
12 | > The Client's Transport typically has internal state (cached TCP connections), | |
13 | so Clients should be reused instead of created as needed. Clients are safe for | |
14 | concurrent use by multiple goroutines. | |
15 | ||
16 | Unfortunately, this is a shared value, and it is not uncommon for libraries to | |
17 | assume that they are free to modify it at will. With enough dependencies, it | |
18 | can be very easy to encounter strange problems and race conditions due to | |
19 | manipulation of this shared value across libraries and goroutines (clients are | |
20 | safe for concurrent use, but writing values to the client struct itself is not | |
21 | protected). | |
22 | ||
23 | Making things worse is the fact that a bare `http.Client` will use a default | |
24 | `http.Transport` called `http.DefaultTransport`, which is another global value | |
25 | that behaves the same way. So it is not simply enough to replace | |
26 | `http.DefaultClient` with `&http.Client{}`. | |
27 | ||
28 | This repository provides some simple functions to get a "clean" `http.Client` | |
29 | -- one that uses the same default values as the Go standard library, but | |
30 | returns a client that does not share any state with other clients. |