-
Notifications
You must be signed in to change notification settings - Fork 6
/
client.go
288 lines (234 loc) · 7.22 KB
/
client.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package meniscus
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"sync"
"time"
)
//HTTPClient ...
type HTTPClient interface {
Do(*http.Request) (*http.Response, error)
}
//BulkClient ...
type BulkClient struct {
httpclient HTTPClient
timeout time.Duration
}
type requestParcel struct {
request *http.Request
index int
}
type roundTripParcel struct {
response *http.Response
request *http.Request // this is required to recreate a http.Response with a new http.Request without a context
err error
index int
}
//NewBulkHTTPClient ...
func NewBulkHTTPClient(client HTTPClient, timeout time.Duration) *BulkClient {
return &BulkClient{
httpclient: client,
timeout: timeout,
}
}
type roundTripChannels struct {
requestList chan requestParcel
receivedResponses chan roundTripParcel
processedResponses chan roundTripParcel
collectResponses chan []roundTripParcel
}
func newRoundTripChannels() roundTripChannels {
return roundTripChannels{
requestList: make(chan requestParcel),
receivedResponses: make(chan roundTripParcel),
processedResponses: make(chan roundTripParcel),
collectResponses: make(chan []roundTripParcel),
}
}
//Do ...
func (cl *BulkClient) Do(bulkRequest *RoundTrip) ([]*http.Response, []error) {
noOfRequests := len(bulkRequest.requests)
if noOfRequests == 0 {
return nil, []error{ErrNoRequests}
}
bulkRequest.responses = make([]*http.Response, noOfRequests)
bulkRequest.errors = make([]error, noOfRequests)
roundTripChannels := newRoundTripChannels()
stopProcessing := make(chan struct{})
defer close(stopProcessing)
ctx, cancel := context.WithTimeout(context.Background(), cl.timeout)
defer cancel()
for index, req := range bulkRequest.requests {
bulkRequest.requests[index] = req.WithContext(ctx)
}
go cl.responseMux(ctx,
bulkRequest,
roundTripChannels.processedResponses,
roundTripChannels.collectResponses)
go cl.workerManager(ctx,
bulkRequest,
&roundTripChannels,
stopProcessing)
cl.completionListener(bulkRequest, roundTripChannels.collectResponses)
return bulkRequest.responses, bulkRequest.errors
}
func (cl *BulkClient) completionListener(bulkRequest *RoundTrip, collectResponses chan []roundTripParcel) {
responses := <-collectResponses
for _, resParcel := range responses {
if resParcel.err != nil {
bulkRequest.updateErrorForIndex(resParcel.err, resParcel.index)
} else {
bulkRequest.updateResponseForIndex(resParcel.response, resParcel.index)
}
}
close(collectResponses)
bulkRequest.addRequestIgnoredErrors()
}
func (cl *BulkClient) responseMux(ctx context.Context,
bulkRequest *RoundTrip,
processedResponses <-chan roundTripParcel, collectResponses chan<- []roundTripParcel) {
var arrayOfResponses []roundTripParcel
LOOP:
for done := 0; done < len(bulkRequest.requests); {
select {
case <-ctx.Done():
break LOOP
case resParcel, isOpen := <-processedResponses:
if isOpen {
arrayOfResponses = append(arrayOfResponses, resParcel)
done++
} else {
break LOOP
}
}
}
collectResponses <- arrayOfResponses
}
func (cl *BulkClient) workerManager(ctx context.Context, bulkRequest *RoundTrip, roundTripChannels *roundTripChannels, stopProcessing chan struct{}) {
var publishWg, fireWg, processWg sync.WaitGroup
publishWg.Add(1)
go bulkRequest.publishAllRequests(roundTripChannels.requestList,
stopProcessing,
&publishWg)
cl.fireRequestsManager(bulkRequest.fireRequestsWorkers,
roundTripChannels.requestList,
roundTripChannels.receivedResponses,
stopProcessing,
&fireWg)
cl.processRequestsManager(ctx,
bulkRequest.processResponseWorkers,
roundTripChannels.receivedResponses,
roundTripChannels.processedResponses,
stopProcessing,
&processWg)
publishWg.Wait()
close(roundTripChannels.requestList)
fireWg.Wait()
close(roundTripChannels.receivedResponses)
processWg.Wait()
close(roundTripChannels.processedResponses)
}
func (cl *BulkClient) fireRequestsManager(fireRequestsWorkers int,
requestList <-chan requestParcel,
recievedResponses chan<- roundTripParcel,
stopProcessing <-chan struct{},
fireWg *sync.WaitGroup) {
for nWorker := 0; nWorker < fireRequestsWorkers; nWorker++ {
fireWg.Add(1)
go cl.fireRequests(requestList, recievedResponses, stopProcessing, fireWg)
}
}
func (cl *BulkClient) processRequestsManager(ctx context.Context,
processResponseWorkers int,
recievedResponses <-chan roundTripParcel, processedResponses chan<- roundTripParcel,
stopProcessing <-chan struct{}, processWg *sync.WaitGroup) {
for mWorker := 0; mWorker < processResponseWorkers; mWorker++ {
processWg.Add(1)
go cl.processRequests(ctx, recievedResponses, processedResponses, stopProcessing, processWg)
}
}
func (cl *BulkClient) fireRequests(reqList <-chan requestParcel,
receivedResponses chan<- roundTripParcel,
stopProcessing <-chan struct{},
fireWg *sync.WaitGroup) {
LOOP:
for reqParcel := range reqList {
result := cl.executeRequest(reqParcel)
select {
case receivedResponses <- result:
case <-stopProcessing:
if result.response != nil {
io.Copy(ioutil.Discard, result.response.Body)
result.response.Body.Close()
}
break LOOP
}
}
fireWg.Done()
}
func (cl *BulkClient) executeRequest(reqParcel requestParcel) roundTripParcel {
resp, err := cl.httpclient.Do(reqParcel.request)
return roundTripParcel{
request: reqParcel.request,
response: resp,
err: err,
index: reqParcel.index,
}
}
func (cl *BulkClient) processRequests(ctx context.Context,
resList <-chan roundTripParcel,
processedResponses chan<- roundTripParcel,
stopProcessing <-chan struct{},
processWg *sync.WaitGroup) {
LOOP:
for resParcel := range resList {
result := cl.parseResponse(ctx, resParcel)
select {
case processedResponses <- result:
case <-stopProcessing:
break LOOP
}
}
processWg.Done()
}
// parseResponse and recreate a Response object with a new Request object (without a timeout).
// It is easy to read from the response object later after we're done processing all requests or we timeout.
// We do not want to be reading from a response for which the request has been canceled.
// We simply close the original response at the end of this function.
func (cl *BulkClient) parseResponse(ctx context.Context, res roundTripParcel) roundTripParcel {
if res.response != nil {
defer res.response.Body.Close()
}
if res.err != nil && (ctx.Err() == context.Canceled || ctx.Err() == context.DeadlineExceeded) {
return roundTripParcel{err: ErrRequestIgnored, index: res.index}
}
if res.err != nil {
return roundTripParcel{err: fmt.Errorf("http client error: %s", res.err), index: res.index}
}
if res.response == nil {
return roundTripParcel{err: errors.New("no response received"), index: res.index}
}
bs, err := ioutil.ReadAll(res.response.Body)
if err != nil {
return roundTripParcel{err: fmt.Errorf("error while reading response body: %s", err), index: res.index}
}
body := ioutil.NopCloser(bytes.NewReader(bs))
newResponse := http.Response{
Body: body,
StatusCode: res.response.StatusCode,
Status: res.response.Status,
Header: res.response.Header,
Request: res.request.WithContext(context.Background()),
}
result := roundTripParcel{
response: &newResponse,
err: err,
index: res.index,
}
return result
}