-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.go
280 lines (272 loc) · 9.57 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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package ltick
import (
"net"
"net/http"
"time"
"github.com/ltick/tick-framework/metrics"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
const (
DEFAULT_DIAL_TIMEOUT = 3 * time.Second
DEFAULT_DIAL_KEEP_ALIVE = 30 * time.Second
DEFAULT_IDLE_CONN_TIMEOUT = 90 * time.Second
DEFAULT_TLS_HANDSHAKE_TIMEOUT = 2 * time.Second
DEFAULT_EXPECT_CONTINUE_TIMEOUT = 1 * time.Second
DEFAULT_MAX_IDLE_CONNS = 1000
DEFAULT_MAX_IDLE_CONNS_PER_HOST = 1000
)
type (
ClientOption func(*ClientOptions)
ClientOptions struct {
Timeout time.Duration
DialTimeout time.Duration
DialKeepAlive time.Duration
IdleConnTimeout time.Duration
TLSHandshakeTimeout time.Duration
ExpectContinueTimeout time.Duration
MaxIdleConns int
MaxIdleConnsPerHost int
MetricsHttpClientRequestsInFlight prometheus.Gauge
MetricsHttpClientRequestsCounter *prometheus.CounterVec
MetricsHttpClientRequestsDurations []prometheus.ObserverVec
// observer of get/put connection from idle pool
MetricsHttpClientRequestsTraceConnection []prometheus.ObserverVec
// observer of dns
MetricsHttpClientRequestsTraceDns []prometheus.ObserverVec
// observer of connect
MetricsHttpClientRequestsTraceConnect []prometheus.ObserverVec
// observer of tls handshake
MetricsHttpClientRequestsTraceTls []prometheus.ObserverVec
// observer of send request
MetricsHttpClientRequestsTraceRequest []prometheus.ObserverVec
MetricsHttpClientRequestLabelFuncs []metrics.HttpClientRequestLabelFunc
}
Client struct {
*ClientOptions
}
)
func ClientTimeout(timeout time.Duration) ClientOption {
return func(options *ClientOptions) {
options.Timeout = timeout
}
}
func ClientDialTimeout(dialTimeout time.Duration) ClientOption {
return func(options *ClientOptions) {
options.DialTimeout = dialTimeout
}
}
func ClientDialKeepAlive(dialKeepAlive time.Duration) ClientOption {
return func(options *ClientOptions) {
options.DialKeepAlive = dialKeepAlive
}
}
func ClientIdleConnTimeout(idleConnTimeout time.Duration) ClientOption {
return func(options *ClientOptions) {
options.IdleConnTimeout = idleConnTimeout
}
}
func ClientTLSHandshakeTimeout(tlsHandshakeTimeout time.Duration) ClientOption {
return func(options *ClientOptions) {
options.TLSHandshakeTimeout = tlsHandshakeTimeout
}
}
func ClientExpectContinueTimeout(expectContinueTimeout time.Duration) ClientOption {
return func(options *ClientOptions) {
options.ExpectContinueTimeout = expectContinueTimeout
}
}
func ClientMaxIdleConns(maxIdleConns int) ClientOption {
return func(options *ClientOptions) {
options.MaxIdleConns = maxIdleConns
}
}
func ClientMaxIdleConnsPerHost(maxIdleConnsPerHost int) ClientOption {
return func(options *ClientOptions) {
options.MaxIdleConnsPerHost = maxIdleConnsPerHost
}
}
func ClientMetricsHttpClientRequestsInFlight(gauge prometheus.Gauge) ClientOption {
if gauge == nil {
gauge = defaultMetricsHttpClientRequestsInFlight
}
return func(options *ClientOptions) {
prometheus.Register(gauge)
options.MetricsHttpClientRequestsInFlight = gauge
}
}
func ClientMetricsHttpClientRequestsCounter(counter *prometheus.CounterVec) ClientOption {
if counter == nil {
counter = defaultMetricsHttpClientRequestsCounter
}
return func(options *ClientOptions) {
prometheus.Register(counter)
options.MetricsHttpClientRequestsCounter = counter
}
}
func ClientMetricsHttpClientRequestsDuration(observers []prometheus.ObserverVec) ClientOption {
if observers == nil {
observers = []prometheus.ObserverVec{defaultMetricsHttpClientRequests}
}
return func(options *ClientOptions) {
for _, observer := range observers {
prometheus.Register(observer)
}
options.MetricsHttpClientRequestsDurations = observers
}
}
func ClientMetricsHttpClientRequestsTraceConnection(observers []prometheus.ObserverVec) ClientOption {
if observers == nil {
observers = []prometheus.ObserverVec{defaultMetricsHttpClientRequestsTraceConnection}
}
return func(options *ClientOptions) {
for _, observer := range observers {
prometheus.Register(observer)
}
options.MetricsHttpClientRequestsTraceConnection = observers
}
}
func ClientMetricsHttpClientRequestsTraceDns(observers []prometheus.ObserverVec) ClientOption {
if observers == nil {
observers = []prometheus.ObserverVec{defaultMetricsHttpClientRequestsTraceDns}
}
return func(options *ClientOptions) {
for _, observer := range observers {
prometheus.Register(observer)
}
options.MetricsHttpClientRequestsTraceDns = observers
}
}
func ClientMetricsHttpClientRequestsTraceConnect(observers []prometheus.ObserverVec) ClientOption {
if observers == nil {
observers = []prometheus.ObserverVec{defaultMetricsHttpClientRequestsTraceConnect}
}
return func(options *ClientOptions) {
for _, observer := range observers {
prometheus.Register(observer)
}
options.MetricsHttpClientRequestsTraceConnect = observers
}
}
func ClientMetricsHttpClientRequestsTraceTls(observers []prometheus.ObserverVec) ClientOption {
if observers == nil {
observers = []prometheus.ObserverVec{defaultMetricsHttpClientRequestsTraceTls}
}
return func(options *ClientOptions) {
for _, observer := range observers {
prometheus.Register(observer)
}
options.MetricsHttpClientRequestsTraceTls = observers
}
}
func ClientMetricsHttpClientRequestsTraceRequest(observers []prometheus.ObserverVec) ClientOption {
if observers == nil {
observers = []prometheus.ObserverVec{defaultMetricsHttpClientRequestsTraceRequest}
}
return func(options *ClientOptions) {
for _, observer := range observers {
prometheus.Register(observer)
}
options.MetricsHttpClientRequestsTraceRequest = observers
}
}
func ClientMetricsHttpClientRequestLabelFunc(httpClientRequestLabelFuncs ...metrics.HttpClientRequestLabelFunc) ClientOption {
return func(options *ClientOptions) {
options.MetricsHttpClientRequestLabelFuncs = httpClientRequestLabelFuncs
}
}
func NewHttpClient(setters ...ClientOption) *http.Client {
c := Client{
&ClientOptions{},
}
for _, setter := range setters {
setter(c.ClientOptions)
}
if c.DialTimeout == 0 {
c.DialTimeout = DEFAULT_DIAL_TIMEOUT
}
if c.DialKeepAlive == 0 {
c.DialKeepAlive = DEFAULT_DIAL_KEEP_ALIVE
}
if c.IdleConnTimeout == 0 {
c.IdleConnTimeout = DEFAULT_IDLE_CONN_TIMEOUT
}
if c.TLSHandshakeTimeout == 0 {
c.TLSHandshakeTimeout = DEFAULT_TLS_HANDSHAKE_TIMEOUT
}
if c.ExpectContinueTimeout == 0 {
c.ExpectContinueTimeout = DEFAULT_EXPECT_CONTINUE_TIMEOUT
}
if c.ExpectContinueTimeout == 0 {
c.ExpectContinueTimeout = DEFAULT_EXPECT_CONTINUE_TIMEOUT
}
if c.MaxIdleConns == 0 {
c.MaxIdleConns = DEFAULT_MAX_IDLE_CONNS
}
if c.MaxIdleConnsPerHost == 0 {
c.MaxIdleConnsPerHost = DEFAULT_MAX_IDLE_CONNS_PER_HOST
}
httpClient := &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: c.DialTimeout,
KeepAlive: c.DialKeepAlive,
}).DialContext,
MaxIdleConns: c.MaxIdleConns,
IdleConnTimeout: c.IdleConnTimeout,
MaxIdleConnsPerHost: c.MaxIdleConnsPerHost,
TLSHandshakeTimeout: c.TLSHandshakeTimeout,
ExpectContinueTimeout: c.ExpectContinueTimeout,
},
}
if c.Timeout > 0 {
httpClient.Timeout = c.Timeout
}
// Wrap the default RoundTripper with middleware.
if c.MetricsHttpClientRequestsDurations != nil {
if c.MetricsHttpClientRequestLabelFuncs != nil {
for _, metricsHttpClientRequestLabelFunc := range c.MetricsHttpClientRequestLabelFuncs {
httpClient.Transport = metrics.InstrumentHttpClientRequest(c.MetricsHttpClientRequestsDurations, httpClient.Transport, metricsHttpClientRequestLabelFunc)
}
} else {
httpClient.Transport = metrics.InstrumentHttpClientRequest(c.MetricsHttpClientRequestsDurations, httpClient.Transport)
}
}
observers := map[string][]prometheus.ObserverVec{}
if c.MetricsHttpClientRequestsTraceConnection != nil {
observers["connection"] = c.MetricsHttpClientRequestsTraceConnection
}
if c.MetricsHttpClientRequestsTraceDns != nil {
observers["dns"] = c.MetricsHttpClientRequestsTraceDns
}
if c.MetricsHttpClientRequestsTraceConnect != nil {
observers["connect"] = c.MetricsHttpClientRequestsTraceConnect
}
if c.MetricsHttpClientRequestsTraceTls != nil {
observers["tls"] = c.MetricsHttpClientRequestsTraceTls
}
if c.MetricsHttpClientRequestsTraceRequest != nil {
observers["request"] = c.MetricsHttpClientRequestsTraceRequest
}
if c.MetricsHttpClientRequestLabelFuncs != nil {
for _, metricsHttpClientRequestLabelFunc := range c.MetricsHttpClientRequestLabelFuncs {
httpClient.Transport = metrics.InstrumentHttpClientRequestTrace(observers, httpClient.Transport, metricsHttpClientRequestLabelFunc)
}
} else {
httpClient.Transport = metrics.InstrumentHttpClientRequestTrace(observers, httpClient.Transport)
}
if c.MetricsHttpClientRequestsCounter != nil {
if c.MetricsHttpClientRequestLabelFuncs != nil {
for _, metricsHttpClientRequestLabelFunc := range c.MetricsHttpClientRequestLabelFuncs {
httpClient.Transport = metrics.InstrumentHttpClientRequestCounter(c.MetricsHttpClientRequestsCounter, httpClient.Transport, metricsHttpClientRequestLabelFunc)
}
} else {
httpClient.Transport = metrics.InstrumentHttpClientRequestCounter(c.MetricsHttpClientRequestsCounter, httpClient.Transport)
}
}
if c.MetricsHttpClientRequestsInFlight != nil {
httpClient.Transport = promhttp.InstrumentRoundTripperInFlight(c.MetricsHttpClientRequestsInFlight, httpClient.Transport)
}
return httpClient
}