-
Notifications
You must be signed in to change notification settings - Fork 4
/
transport.go
179 lines (157 loc) · 5.32 KB
/
transport.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
package transport
import (
"context"
"crypto/tls"
"net"
"net/http"
"net/url"
"time"
)
// Factory is any function that takes no arguments and returns a Transport.
type Factory func() http.RoundTripper
// Option is a function that either modifies or generates a new Transport.
type Option func(*http.Transport) *http.Transport
// OptionProxy installs a custom Proxy configuration in the Transport.
func OptionProxy(proxy func(*http.Request) (*url.URL, error)) Option {
return func(t *http.Transport) *http.Transport {
t.Proxy = proxy
return t
}
}
// OptionDialContext installs a custom DialContext configuration in the Transport.
func OptionDialContext(dialCtx func(ctx context.Context, network, addr string) (net.Conn, error)) Option {
return func(t *http.Transport) *http.Transport {
t.DialContext = dialCtx
return t
}
}
// OptionDial installs a custom Dial configuration in the Transport.
func OptionDial(dial func(network, addr string) (net.Conn, error)) Option {
return func(t *http.Transport) *http.Transport {
t.Dial = dial // nolint:go-lint,staticcheck
return t
}
}
// OptionDialTLS installs a custom DialTLS configuration in the Transport.
func OptionDialTLS(dial func(network, addr string) (net.Conn, error)) Option {
return func(t *http.Transport) *http.Transport {
t.DialTLS = dial // nolint:go-lint,staticcheck
return t
}
}
// OptionTLSClientConfig installs a custom TLSClientConfig in the Transport.
func OptionTLSClientConfig(config *tls.Config) Option {
return func(t *http.Transport) *http.Transport {
t.TLSClientConfig = config
return t
}
}
// OptionTLSHandshakeTimeout installs a custom TLSHandshakeTimeout in the Transport.
func OptionTLSHandshakeTimeout(timeout time.Duration) Option {
return func(t *http.Transport) *http.Transport {
t.TLSHandshakeTimeout = timeout
return t
}
}
// OptionDisableKeepAlives installs a custom DisableKeepAlives option in the Transport.
func OptionDisableKeepAlives(disabled bool) Option {
return func(t *http.Transport) *http.Transport {
t.DisableKeepAlives = disabled
return t
}
}
// OptionDisableCompression installs a custom DisableCompression option in the Transport.
func OptionDisableCompression(disabled bool) Option {
return func(t *http.Transport) *http.Transport {
t.DisableCompression = disabled
return t
}
}
// OptionMaxIdleConns installs a custom MaxIdleConns option in the Transport.
func OptionMaxIdleConns(max int) Option {
return func(t *http.Transport) *http.Transport {
t.MaxIdleConns = max
return t
}
}
// OptionMaxIdleConnsPerHost installs a custom MaxIdleConnsPerHost option in the Transport.
func OptionMaxIdleConnsPerHost(max int) Option {
return func(t *http.Transport) *http.Transport {
t.MaxIdleConnsPerHost = max
return t
}
}
// OptionIdleConnTimeout installs a custom IdleConnTimeout option in the Transport.
func OptionIdleConnTimeout(timeout time.Duration) Option {
return func(t *http.Transport) *http.Transport {
t.IdleConnTimeout = timeout
return t
}
}
// OptionResponseHeaderTimeout installs a custom ResponseHeaderTimeout option in the Transport.
func OptionResponseHeaderTimeout(timeout time.Duration) Option {
return func(t *http.Transport) *http.Transport {
t.ResponseHeaderTimeout = timeout
return t
}
}
// OptionExpectContinueTimeout installs a custom ExpectContinueTimeout option in the Transport.
func OptionExpectContinueTimeout(timeout time.Duration) Option {
return func(t *http.Transport) *http.Transport {
t.ExpectContinueTimeout = timeout
return t
}
}
// OptionTLSNextProto installs a custom TLSNextProto option in the Transport.
func OptionTLSNextProto(next map[string]func(authority string, c *tls.Conn) http.RoundTripper) Option {
return func(t *http.Transport) *http.Transport {
t.TLSNextProto = next
return t
}
}
// OptionProxyConnectHeader installs a custom ProxyConnectHeader option in the Transport.
func OptionProxyConnectHeader(header http.Header) Option {
return func(t *http.Transport) *http.Transport {
t.ProxyConnectHeader = header
return t
}
}
// OptionMaxResponseHeaderBytes installs a custom MaxResponseHeaderBytes option in the Transport.
func OptionMaxResponseHeaderBytes(max int64) Option {
return func(t *http.Transport) *http.Transport {
t.MaxResponseHeaderBytes = max
return t
}
}
// OptionDefaultTransport configures a transport to match the http.DefaultTransport.
//
// Deprecated: The New() method uses a copy of the http.DefaultTransport as the
// base transport on which options are applied. This option is redundant and
// can be removed wherever it is used.
func OptionDefaultTransport(t *http.Transport) *http.Transport {
t = OptionProxy(http.ProxyFromEnvironment)(t)
t = OptionDialContext((&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}).DialContext)(t)
t = OptionMaxIdleConns(100)(t)
t = OptionIdleConnTimeout(90 * time.Second)(t)
t = OptionTLSHandshakeTimeout(10 * time.Second)(t)
t = OptionExpectContinueTimeout(time.Second)(t)
return t
}
// New applies the given options to a Transport and returns it.
func New(opts ...Option) *http.Transport {
var t = http.DefaultTransport.(*http.Transport).Clone()
for _, opt := range opts {
t = opt(t)
}
return t
}
// NewFactory returns a Factory that is bound to the given Option set.
func NewFactory(opts ...Option) Factory {
return func() http.RoundTripper {
return New(opts...)
}
}