-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.go
52 lines (47 loc) · 1.39 KB
/
http.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
package httpify
import (
"crypto/tls"
"net"
"net/http"
"time"
)
// HostSprayingTransport returns a new http.Transport with disabled idle connections and keepalives.
func NoKeepAliveTransport() *http.Transport {
transport := PooledTransport()
transport.DisableKeepAlives = true
transport.MaxIdleConnsPerHost = -1
return transport
}
// PooledTransport returns a new http.Transport for connection reuse.
func PooledTransport() *http.Transport {
return &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}).DialContext,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
MaxIdleConnsPerHost: 100,
MaxResponseHeaderBytes: 4096, // Default is 10MB
TLSClientConfig: &tls.Config{
Renegotiation: tls.RenegotiateOnceAsClient,
InsecureSkipVerify: true, // Optional, but unsafe
},
}
}
// DefaultClient creates a new http.Client with disabled idle connections and keepalives.
func DefaultClient() *http.Client {
return &http.Client{
Transport: NoKeepAliveTransport(),
}
}
// DefaultPooledClient returns an http.Client with a pooled transport for connection reuse.
func DefaultPooledClient() *http.Client {
return &http.Client{
Transport: PooledTransport(),
}
}