forked from palantir/conjure-go-runtime
/
client_builder.go
201 lines (184 loc) · 5.86 KB
/
client_builder.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
// Copyright (c) 2018 Palantir Technologies. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package httpclient provides round trippers/transport wrappers for http clients.
package httpclient
import (
"crypto/tls"
"net"
"net/http"
"net/url"
"time"
"github.com/palantir/pkg/bytesbuffers"
"github.com/palantir/pkg/retry"
"github.com/palantir/pkg/tlsconfig"
werror "github.com/palantir/witchcraft-go-error"
"golang.org/x/net/http2"
"golang.org/x/net/proxy"
)
type clientBuilder struct {
httpClientBuilder
uris []string
maxRetries int
backoffOptions []retry.Option
errorDecoder ErrorDecoder
disableTraceHeaderPropagation bool
}
type httpClientBuilder struct {
ServiceName string
// http.Client modifiers
Timeout time.Duration
Middlewares []Middleware
metricsMiddleware Middleware
// http.Transport modifiers
MaxIdleConns int
MaxIdleConnsPerHost int
Proxy func(*http.Request) (*url.URL, error)
ProxyDialerBuilder func(*net.Dialer) (proxy.Dialer, error)
TLSClientConfig *tls.Config
DisableHTTP2 bool
DisableRecovery bool
DisableTracing bool
IdleConnTimeout time.Duration
TLSHandshakeTimeout time.Duration
ExpectContinueTimeout time.Duration
ResponseHeaderTimeout time.Duration
// http.Dialer modifiers
DialTimeout time.Duration
KeepAlive time.Duration
EnableIPV6 bool
BytesBufferPool bytesbuffers.Pool
}
// NewClient returns a configured client ready for use.
// We apply "sane defaults" before applying the provided params.
func NewClient(params ...ClientParam) (Client, error) {
b := &clientBuilder{
httpClientBuilder: *getDefaultHTTPClientBuilder(),
backoffOptions: []retry.Option{retry.WithInitialBackoff(250 * time.Millisecond)},
errorDecoder: restErrorDecoder{},
}
for _, p := range params {
if p == nil {
continue
}
if err := p.apply(b); err != nil {
return nil, err
}
}
client, middlewares, err := httpClientAndRoundTripHandlersFromBuilder(&b.httpClientBuilder)
if err != nil {
return nil, err
}
var edm Middleware
if b.errorDecoder != nil {
edm = errorDecoderMiddleware(b.errorDecoder)
}
if b.maxRetries == 0 {
b.maxRetries = 2 * len(b.uris)
}
return &clientImpl{
client: *client,
uris: b.uris,
maxRetries: b.maxRetries,
backoffOptions: b.backoffOptions,
disableTraceHeaderPropagation: b.disableTraceHeaderPropagation,
middlewares: middlewares,
metricsMiddleware: b.metricsMiddleware,
errorDecoderMiddleware: edm,
}, nil
}
func getDefaultHTTPClientBuilder() *httpClientBuilder {
defaultTLSConfig, _ := tlsconfig.NewClientConfig()
return &httpClientBuilder{
// These values are primarily pulled from http.DefaultTransport.
TLSClientConfig: defaultTLSConfig,
Timeout: 1 * time.Minute,
DialTimeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
MaxIdleConns: 32,
MaxIdleConnsPerHost: 32,
EnableIPV6: false,
DisableHTTP2: false,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}
}
// NewHTTPClient returns a configured http client ready for use.
// We apply "sane defaults" before applying the provided params.
func NewHTTPClient(params ...HTTPClientParam) (*http.Client, error) {
b := getDefaultHTTPClientBuilder()
for _, p := range params {
if p == nil {
continue
}
if err := p.applyHTTPClient(b); err != nil {
return nil, err
}
}
client, roundTrippers, err := httpClientAndRoundTripHandlersFromBuilder(b)
if err != nil {
return nil, err
}
if b.metricsMiddleware != nil {
client.Transport = wrapTransport(client.Transport, b.metricsMiddleware)
}
for _, handler := range roundTrippers {
client.Transport = wrapTransport(client.Transport, handler)
}
return client, err
}
func httpClientAndRoundTripHandlersFromBuilder(b *httpClientBuilder) (*http.Client, []Middleware, error) {
dialer := &net.Dialer{
Timeout: b.DialTimeout,
KeepAlive: b.KeepAlive,
DualStack: b.EnableIPV6,
}
transport := &http.Transport{
DialContext: dialer.DialContext,
MaxIdleConns: b.MaxIdleConns,
MaxIdleConnsPerHost: b.MaxIdleConnsPerHost,
Proxy: b.Proxy,
TLSClientConfig: b.TLSClientConfig,
ExpectContinueTimeout: b.ExpectContinueTimeout,
IdleConnTimeout: b.IdleConnTimeout,
TLSHandshakeTimeout: b.TLSHandshakeTimeout,
ResponseHeaderTimeout: b.ResponseHeaderTimeout,
}
if b.ProxyDialerBuilder != nil {
// Used for socks5 proxying
// TODO: use DialContext if x/proxy ever supports it
proxyDialer, err := b.ProxyDialerBuilder(dialer)
if err != nil {
return nil, nil, err
}
transport.Dial = proxyDialer.Dial
transport.DialContext = nil
transport.Proxy = nil
}
if !b.DisableHTTP2 {
if err := http2.ConfigureTransport(transport); err != nil {
return nil, nil, werror.Wrap(err, "failed to configure transport for http2")
}
}
if !b.DisableTracing {
_ = WithMiddleware(&traceMiddleware{ServiceName: b.ServiceName}).applyHTTPClient(b)
}
if !b.DisableRecovery {
_ = WithMiddleware(&recoveryMiddleware{}).applyHTTPClient(b)
}
return &http.Client{
Timeout: b.Timeout,
Transport: transport,
}, b.Middlewares, nil
}