-
Notifications
You must be signed in to change notification settings - Fork 11
/
httpclient.go
92 lines (83 loc) · 3.23 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package j8a
import (
"crypto/tls"
"net"
"net/http"
"os"
"runtime"
"time"
"github.com/rs/zerolog/log"
)
type HTTPClient interface {
Do(req *http.Request) (*http.Response, error)
Get(uri string) (*http.Response, error)
}
// scaffoldHTTPClient is a factory method that applies connection params to the transport layer of http.Client
func scaffoldHTTPClient(runtime *Runtime) HTTPClient {
idleConnTimeoutDuration := time.Duration(runtime.Connection.Upstream.IdleTimeoutSeconds) * time.Second
tLSHandshakeTimeoutDuration := time.Duration(runtime.Connection.Upstream.SocketTimeoutSeconds) * time.Second
socketTimeoutDuration := time.Duration(runtime.Connection.Upstream.SocketTimeoutSeconds) * time.Second
readTimeoutDuration := time.Duration(runtime.Connection.Upstream.ReadTimeoutSeconds) * time.Second
tlsInsecureSkipVerify := runtime.Connection.Upstream.TlsInsecureSkipVerify
httpClient = &http.Client{
Transport: &http.Transport{
DisableCompression: true,
DialContext: (&net.Dialer{
Timeout: socketTimeoutDuration,
KeepAlive: getKeepAliveIntervalDuration(),
}).DialContext,
//TLS handshake timeout is the same as connection timeout
TLSHandshakeTimeout: tLSHandshakeTimeoutDuration,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: tlsInsecureSkipVerify,
},
MaxIdleConns: runtime.Connection.Upstream.PoolSize,
MaxIdleConnsPerHost: runtime.Connection.Upstream.PoolSize,
IdleConnTimeout: idleConnTimeoutDuration,
},
//Timeout: readTimeoutDuration, don't use this anymore, proxyHandler now has it built-in
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
log.Info().
Int("upMaxIdleConns", runtime.Connection.Upstream.PoolSize).
Int("upMaxIdleConnsPerHost", runtime.Connection.Upstream.PoolSize).
Float64("upTransportDialTimeoutSecs", socketTimeoutDuration.Seconds()).
Float64("upTlsHandshakeTimeoutSecs", tLSHandshakeTimeoutDuration.Seconds()).
Float64("upIdleConnTimeoutSecs", idleConnTimeoutDuration.Seconds()).
Float64("upReadTimeoutSecs", readTimeoutDuration.Seconds()).
Float64("upTransportDialKeepAliveIntervalSecs", getKeepAliveIntervalDuration().Seconds()).
Bool("upTlsInsecureSkipVerify", tlsInsecureSkipVerify).
Msg("server derived upstream params")
return httpClient
}
// getKeepAliveIntervalSecondsDuration. KeepAlive is effectively: initial delay + interval * TCP_KEEPCNT (9 on linux, 8 ox OSX).
// The KeepAliveIntervalSecondsDuration here defines interval, i.e. default 15s * 9 = 135s on linux
// See: https://github.com/golang/go/issues/23459#issuecomment-374777402
// The OS uses zero payload TCP segments to attempt to keep the connection alive.
// after the total number of unacknowledged TCP_KEEPCNT is reached, the dialer kills the
// connection.
func getKeepAliveIntervalDuration() time.Duration {
return time.Duration(float64(Runner.
Connection.
Upstream.
IdleTimeoutSeconds) / float64(getTCPKeepCnt()) * float64(time.Second))
}
func getTCPKeepCnt() int {
os := os.Getenv("OS")
if len(os) == 0 {
os = runtime.GOOS
}
switch os {
case "windows":
return 5
case "darwin", "freebsd", "openbsd":
return 8
case "linux":
return 9
//if we don't know, assume some kind of linux
default:
return 9
}
}