/
client.go
56 lines (48 loc) · 1.2 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package http
import (
"crypto/tls"
"net/http"
"time"
)
type ClientOpts struct {
Verbose bool
Insecure bool
Timeout time.Duration
}
func ClientFromOpts(opts ClientOpts) *http.Client {
transport := defaultTransport()
if opts.Insecure {
tlsConfig := &tls.Config{
InsecureSkipVerify: true,
}
transport = &http.Transport{
Proxy: http.ProxyFromEnvironment,
TLSClientConfig: tlsConfig,
}
}
if opts.Verbose {
transport = &verboseTracer{transport}
}
timeout := 20 * time.Second
if opts.Timeout > 0 {
timeout = opts.Timeout
}
return &http.Client{
Transport: transport,
Timeout: timeout,
}
}
// defaultTransport is the default implementation of Transport and is
// used by DefaultClient. It establishes network connections as needed
// and caches them for reuse by subsequent calls. It uses HTTP proxies
// as directed by the $HTTP_PROXY and $NO_PROXY (or $http_proxy and
// $no_proxy) environment variables.
func defaultTransport() http.RoundTripper {
t := http.DefaultTransport.(*http.Transport).Clone()
t.MaxIdleConns = 50
t.MaxConnsPerHost = 50
t.MaxIdleConnsPerHost = 50
t.TLSHandshakeTimeout = 20 * time.Second
t.ExpectContinueTimeout = 2 * time.Second
return t
}