-
Notifications
You must be signed in to change notification settings - Fork 1
/
http_connector.go
53 lines (44 loc) · 1.29 KB
/
http_connector.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
package connect
import (
"crypto/tls"
"net/http"
"time"
)
// HTTPConnectionOptions options for the http connection
type HTTPConnectionOptions struct {
TLSHandshakeTimeout time.Duration
TLSInsecureSkipVerify bool
Timeout time.Duration
UseOpenTelemetry bool
EnableKeepAlives bool
}
var defaultHTTPConnectionOptions = &HTTPConnectionOptions{
TLSHandshakeTimeout: 5 * time.Second,
TLSInsecureSkipVerify: false,
Timeout: 200 * time.Second,
UseOpenTelemetry: false,
EnableKeepAlives: true,
}
// NewHTTPConnection new http client
func NewHTTPConnection(opt *HTTPConnectionOptions) *http.Client {
options := applyHTTPConnectionOptions(opt)
httpClient := &http.Client{
Timeout: options.Timeout,
Transport: &http.Transport{
TLSHandshakeTimeout: options.TLSHandshakeTimeout,
TLSClientConfig: &tls.Config{InsecureSkipVerify: options.TLSInsecureSkipVerify}, //nolint:gosec
DisableKeepAlives: !options.EnableKeepAlives,
},
}
if !options.UseOpenTelemetry {
return httpClient
}
httpClient.Transport = NewTransport(WithRoundTripper(httpClient.Transport))
return httpClient
}
func applyHTTPConnectionOptions(opt *HTTPConnectionOptions) *HTTPConnectionOptions {
if opt != nil {
return opt
}
return defaultHTTPConnectionOptions
}