/
http.go
206 lines (174 loc) · 4.94 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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package endpoints
import (
"crypto/x509"
"net/http"
"net/http/cookiejar"
"net/url"
"strings"
"time"
"go.uber.org/zap"
"golang.org/x/net/publicsuffix"
tracehttp "gopkg.in/DataDog/dd-trace-go.v1/contrib/net/http"
"github.com/coinbase/chainstorage/internal/utils/ratelimiter"
)
type (
ClientOption func(opts *clientOptions)
clientOptions struct {
timeout time.Duration
idleConnTimeout time.Duration
maxConns int
jar http.CookieJar
stickySessionHeader *stickySessionHeader
headers map[string]string
rootCAs *x509.CertPool
rps int
}
// stickySessionJar implements passive cookie affinity.
//
// The LB takes a cookie that’s present in the cookies header and hashes on its value.
// Ref: https://www.envoyproxy.io/docs/envoy/latest/api-v3/config/route/v3/route_components.proto#envoy-v3-api-msg-config-route-v3-routeaction-hashpolicy-cookie
stickySessionJar struct {
url string
cookieKey string
cookieValue string
}
stickySessionHeader struct {
headerKey string
headerValue string
}
roundTripper struct {
base http.RoundTripper
logger *zap.Logger
headers map[string]string
rateLimiter *ratelimiter.RateLimiter
}
)
const (
defaultTimeout = 120 * time.Second
defaultIdleConnTimeout = 30 * time.Second
defaultMaxConns = 120
)
var _ http.CookieJar = (*stickySessionJar)(nil)
func newHTTPClient(opts ...ClientOption) (*http.Client, error) {
options := &clientOptions{
timeout: defaultTimeout,
idleConnTimeout: defaultIdleConnTimeout,
maxConns: defaultMaxConns,
}
for _, opt := range opts {
opt(options)
}
var httpClient *http.Client
var err error
if options.jar == nil && options.stickySessionHeader == nil {
httpClient, err = newDefaultClient(options)
if err != nil {
return nil, err
}
} else {
httpClient, err = newStickyClient(options)
if err != nil {
return nil, err
}
}
return tracehttp.WrapClient(httpClient), nil
}
func newDefaultClient(options *clientOptions) (*http.Client, error) {
transport := http.DefaultTransport.(*http.Transport).Clone()
if options.rootCAs != nil {
transport.TLSClientConfig.RootCAs = options.rootCAs
}
client := &http.Client{
Timeout: options.timeout,
Transport: transport,
}
wrapClient := WrapHTTPClient(client, options.headers, options.rps)
return wrapClient, nil
}
func newStickyClient(options *clientOptions) (*http.Client, error) {
transport := http.DefaultTransport.(*http.Transport).Clone()
// Override connection pool options.
transport.IdleConnTimeout = options.idleConnTimeout
transport.MaxIdleConns = options.maxConns
transport.MaxIdleConnsPerHost = options.maxConns
if options.rootCAs != nil {
transport.TLSClientConfig.RootCAs = options.rootCAs
}
client := &http.Client{
Timeout: options.timeout,
Transport: transport,
Jar: options.jar,
}
headers := make(map[string]string)
stickySession := options.stickySessionHeader
if stickySession != nil {
headers[stickySession.headerKey] = stickySession.headerValue
}
for key, val := range options.headers {
headers[key] = val
}
wrapClient := WrapHTTPClient(client, headers, options.rps)
return wrapClient, nil
}
// The HeaderHash method consistently maps a header value to a specific node.
func withStickySessionHeaderHash(key string, value string) ClientOption {
return func(opts *clientOptions) {
opts.stickySessionHeader = &stickySessionHeader{
headerKey: key,
headerValue: value,
}
}
}
// The CookiePassive method persists the cookie value provided by the server.
func withStickySessionCookiePassive() ClientOption {
return func(opts *clientOptions) {
jar, _ := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
opts.jar = jar
}
}
// The CookieHash method consistently maps a cookie value to a specific node.
func withStickySessionCookieHash(url string, key string, value string) ClientOption {
return func(opts *clientOptions) {
opts.jar = &stickySessionJar{
url: url,
cookieKey: key,
cookieValue: value,
}
}
}
// The withHeaders method add customized headers to request.
func withHeaders(headers map[string]string) ClientOption {
return func(opts *clientOptions) {
opts.headers = headers
}
}
func (j *stickySessionJar) SetCookies(_ *url.URL, _ []*http.Cookie) {
// nop
}
func (j *stickySessionJar) Cookies(u *url.URL) []*http.Cookie {
if !strings.HasPrefix(u.String(), j.url) {
return nil
}
return []*http.Cookie{
{
Name: j.cookieKey,
Value: j.cookieValue,
},
}
}
func withOverrideRequestTimeout(timeout time.Duration) ClientOption {
return func(opts *clientOptions) {
opts.timeout = timeout
}
}
// temporary fix cert issue for golang1.18 on MacOS
func withRootCAs(roots *x509.CertPool) ClientOption {
return func(opts *clientOptions) {
opts.rootCAs = roots
}
}
func withRPS(rps int) ClientOption {
return func(opts *clientOptions) {
opts.rps = rps
}
}