forked from codesenberg/bombardier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clients.go
237 lines (202 loc) · 4.71 KB
/
clients.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
package main
import (
"crypto/tls"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
"time"
"github.com/valyala/fasthttp"
"golang.org/x/net/http2"
)
type client interface {
do() (code int, usTaken uint64, err error)
}
type bodyStreamProducer func() (io.ReadCloser, error)
type clientOpts struct {
HTTP2 bool
maxConns uint64
timeout time.Duration
tlsConfig *tls.Config
disableKeepAlives bool
headers *headersList
url, method string
body *string
bodProd bodyStreamProducer
bytesRead, bytesWritten *int64
}
type fasthttpClient struct {
client *fasthttp.HostClient
headers *fasthttp.RequestHeader
host, requestURI, method string
body *string
bodProd bodyStreamProducer
}
func newFastHTTPClient(opts *clientOpts) client {
c := new(fasthttpClient)
u, err := url.Parse(opts.url)
if err != nil {
// opts.url guaranteed to be valid at this point
panic(err)
}
c.host = u.Host
c.requestURI = u.RequestURI()
c.client = &fasthttp.HostClient{
Addr: u.Host,
IsTLS: u.Scheme == "https",
MaxConns: int(opts.maxConns),
ReadTimeout: opts.timeout,
WriteTimeout: opts.timeout,
DisableHeaderNamesNormalizing: true,
TLSConfig: opts.tlsConfig,
Dial: fasthttpDialFunc(
opts.bytesRead, opts.bytesWritten,
),
}
c.headers = headersToFastHTTPHeaders(opts.headers)
c.method, c.body = opts.method, opts.body
c.bodProd = opts.bodProd
return client(c)
}
func (c *fasthttpClient) do() (
code int, usTaken uint64, err error,
) {
// prepare the request
req := fasthttp.AcquireRequest()
resp := fasthttp.AcquireResponse()
if c.headers != nil {
c.headers.CopyTo(&req.Header)
}
if len(req.Header.Host()) == 0 {
req.Header.SetHost(c.host)
}
req.Header.SetMethod(c.method)
if c.client.IsTLS {
req.URI().SetScheme("https")
} else {
req.URI().SetScheme("http")
}
req.SetRequestURI(c.requestURI)
if c.body != nil {
req.SetBodyString(*c.body)
} else {
bs, bserr := c.bodProd()
if bserr != nil {
return 0, 0, bserr
}
req.SetBodyStream(bs, -1)
}
// fire the request
start := time.Now()
err = c.client.Do(req, resp)
if err != nil {
code = -1
} else {
code = resp.StatusCode()
}
usTaken = uint64(time.Since(start).Nanoseconds() / 1000)
// release resources
fasthttp.ReleaseRequest(req)
fasthttp.ReleaseResponse(resp)
return
}
type httpClient struct {
client *http.Client
headers http.Header
url *url.URL
method string
body *string
bodProd bodyStreamProducer
}
func newHTTPClient(opts *clientOpts) client {
c := new(httpClient)
tr := &http.Transport{
TLSClientConfig: opts.tlsConfig,
MaxIdleConnsPerHost: int(opts.maxConns),
DisableKeepAlives: opts.disableKeepAlives,
}
tr.DialContext = httpDialContextFunc(opts.bytesRead, opts.bytesWritten)
if opts.HTTP2 {
_ = http2.ConfigureTransport(tr)
} else {
tr.TLSNextProto = make(
map[string]func(authority string, c *tls.Conn) http.RoundTripper,
)
}
cl := &http.Client{
Transport: tr,
Timeout: opts.timeout,
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
c.client = cl
c.headers = headersToHTTPHeaders(opts.headers)
c.method, c.body, c.bodProd = opts.method, opts.body, opts.bodProd
var err error
c.url, err = url.Parse(opts.url)
if err != nil {
// opts.url guaranteed to be valid at this point
panic(err)
}
return client(c)
}
func (c *httpClient) do() (
code int, usTaken uint64, err error,
) {
req := &http.Request{}
req.Header = c.headers
req.Method = c.method
req.URL = c.url
if host := req.Header.Get("Host"); host != "" {
req.Host = host
}
if c.body != nil {
br := strings.NewReader(*c.body)
req.ContentLength = int64(len(*c.body))
req.Body = ioutil.NopCloser(br)
} else {
bs, bserr := c.bodProd()
if bserr != nil {
return 0, 0, bserr
}
req.Body = bs
}
start := time.Now()
resp, err := c.client.Do(req)
if err != nil {
code = -1
} else {
code = resp.StatusCode
_, berr := io.Copy(ioutil.Discard, resp.Body)
if berr != nil {
err = berr
}
if cerr := resp.Body.Close(); cerr != nil {
err = cerr
}
}
usTaken = uint64(time.Since(start).Nanoseconds() / 1000)
return
}
func headersToFastHTTPHeaders(h *headersList) *fasthttp.RequestHeader {
if len(*h) == 0 {
return nil
}
res := new(fasthttp.RequestHeader)
for _, header := range *h {
res.Set(header.key, header.value)
}
return res
}
func headersToHTTPHeaders(h *headersList) http.Header {
if len(*h) == 0 {
return http.Header{}
}
headers := http.Header{}
for _, header := range *h {
headers[header.key] = []string{header.value}
}
return headers
}