]>
Commit | Line | Data |
---|---|---|
c680a8e1 RS |
1 | package cleanhttp |
2 | ||
3 | import ( | |
4 | "net" | |
5 | "net/http" | |
6 | "runtime" | |
7 | "time" | |
8 | ) | |
9 | ||
10 | // DefaultTransport returns a new http.Transport with similar default values to | |
11 | // http.DefaultTransport, but with idle connections and keepalives disabled. | |
12 | func DefaultTransport() *http.Transport { | |
13 | transport := DefaultPooledTransport() | |
14 | transport.DisableKeepAlives = true | |
15 | transport.MaxIdleConnsPerHost = -1 | |
16 | return transport | |
17 | } | |
18 | ||
19 | // DefaultPooledTransport returns a new http.Transport with similar default | |
20 | // values to http.DefaultTransport. Do not use this for transient transports as | |
21 | // it can leak file descriptors over time. Only use this for transports that | |
22 | // will be re-used for the same host(s). | |
23 | func DefaultPooledTransport() *http.Transport { | |
24 | transport := &http.Transport{ | |
25 | Proxy: http.ProxyFromEnvironment, | |
26 | DialContext: (&net.Dialer{ | |
27 | Timeout: 30 * time.Second, | |
28 | KeepAlive: 30 * time.Second, | |
15c0b25d | 29 | DualStack: true, |
c680a8e1 RS |
30 | }).DialContext, |
31 | MaxIdleConns: 100, | |
32 | IdleConnTimeout: 90 * time.Second, | |
33 | TLSHandshakeTimeout: 10 * time.Second, | |
34 | ExpectContinueTimeout: 1 * time.Second, | |
35 | MaxIdleConnsPerHost: runtime.GOMAXPROCS(0) + 1, | |
36 | } | |
37 | return transport | |
38 | } | |
39 | ||
40 | // DefaultClient returns a new http.Client with similar default values to | |
41 | // http.Client, but with a non-shared Transport, idle connections disabled, and | |
42 | // keepalives disabled. | |
43 | func DefaultClient() *http.Client { | |
44 | return &http.Client{ | |
45 | Transport: DefaultTransport(), | |
46 | } | |
47 | } | |
48 | ||
49 | // DefaultPooledClient returns a new http.Client with similar default values to | |
50 | // http.Client, but with a shared Transport. Do not use this function for | |
51 | // transient clients as it can leak file descriptors over time. Only use this | |
52 | // for clients that will be re-used for the same host(s). | |
53 | func DefaultPooledClient() *http.Client { | |
54 | return &http.Client{ | |
55 | Transport: DefaultPooledTransport(), | |
56 | } | |
57 | } |