-
Notifications
You must be signed in to change notification settings - Fork 26
/
httpClient.go
51 lines (44 loc) · 1.1 KB
/
httpClient.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
package main
import (
"net"
"net/http"
"time"
"github.com/klauspost/compress/gzhttp"
)
func newHttpClient() *http.Client {
return &http.Client{
Timeout: time.Minute,
Transport: newHttpTransport(),
}
}
func newHttpTransport() http.RoundTripper {
return newAddUserAgentTransport(
gzhttp.Transport(
&http.Transport{
// Default
Proxy: http.ProxyFromEnvironment,
ForceAttemptHTTP2: true,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
// Custom
DisableKeepAlives: true,
},
),
)
}
type addUserAgentTransport struct {
parent http.RoundTripper
}
func (t *addUserAgentTransport) RoundTrip(r *http.Request) (*http.Response, error) {
r.Header.Set(userAgent, appUserAgent)
return t.parent.RoundTrip(r)
}
func newAddUserAgentTransport(parent http.RoundTripper) *addUserAgentTransport {
return &addUserAgentTransport{parent}
}