-
Notifications
You must be signed in to change notification settings - Fork 320
/
Copy pathclients.go
221 lines (186 loc) · 4.29 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
package main
import (
"crypto/tls"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
"time"
"github.com/valyala/fasthttp"
)
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
requestURL *url.URL
headers *headersList
method string
body *string
bodProd bodyStreamProducer
bytesRead, bytesWritten *int64
}
type fasthttpClient struct {
client *fasthttp.Client
headers *fasthttp.RequestHeader
uri *fasthttp.URI
method string
body *string
bodProd bodyStreamProducer
}
func newFastHTTPClient(opts *clientOpts) client {
c := new(fasthttpClient)
uri := fasthttp.AcquireURI()
if err := uri.Parse(
[]byte(opts.requestURL.Host),
[]byte(opts.requestURL.String()),
); err != nil {
// opts.requestURL must always be valid
panic(err)
}
c.uri = uri
c.client = &fasthttp.Client{
MaxConnsPerHost: int(opts.maxConns),
ReadTimeout: opts.timeout,
WriteTimeout: opts.timeout,
DisableHeaderNamesNormalizing: true,
TLSConfig: opts.tlsConfig,
Dial: fasthttpDialFunc(
opts.bytesRead, opts.bytesWritten,
opts.timeout,
),
}
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()
req.Header.SetMethod(c.method)
if c.headers != nil {
c.headers.CopyTo(&req.Header)
}
req.SetURI(c.uri)
req.UseHostHeader = true
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,
ForceAttemptHTTP2: opts.HTTP2,
DialContext: httpDialContextFunc(opts.bytesRead, opts.bytesWritten, opts.timeout),
}
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
c.url = opts.requestURL
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
}