/
httputils.go
492 lines (458 loc) · 12.2 KB
/
httputils.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
// Copyright 2022 Google LLC
//
// 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
//
// https://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 client
import (
"bytes"
"context"
"encoding/json"
"fmt"
"github.com/cenkalti/backoff/v4"
"io"
"mime/multipart"
"net/http"
"net/http/httptrace"
"net/http/httputil"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
)
type HTTPHelper struct {
Client *http.Client
RootEndpoint string
Dumpster io.Writer
AccessToken string
HTTPBasicUsername string
}
func (h *HTTPHelper) NewGetRequest(path string) *HTTPRequestBuilder {
req, err := http.NewRequest(http.MethodGet, h.RootEndpoint+path, nil)
return &HTTPRequestBuilder{
helper: h,
request: req,
err: err,
}
}
func (h *HTTPHelper) NewDeleteRequest(path string) *HTTPRequestBuilder {
req, err := http.NewRequest(http.MethodDelete, h.RootEndpoint+path, nil)
return &HTTPRequestBuilder{
helper: h,
request: req,
err: err,
}
}
func (h *HTTPHelper) NewPostRequest(path string, jsonBody any) *HTTPRequestBuilder {
body := []byte{}
var err error
if jsonBody != nil {
if body, err = json.Marshal(jsonBody); err != nil {
return &HTTPRequestBuilder{helper: h, request: nil, err: err}
}
}
var req *http.Request
if req, err = http.NewRequest(http.MethodPost, h.RootEndpoint+path, bytes.NewBuffer(body)); err != nil {
return &HTTPRequestBuilder{helper: h, request: nil, err: err}
}
req.Header.Set("Content-Type", "application/json")
return &HTTPRequestBuilder{
helper: h,
request: req,
err: err,
}
}
func (h *HTTPHelper) NewUploadFileRequest(ctx context.Context, path string, body io.Reader, contentType string) *HTTPRequestBuilder {
req, err := http.NewRequestWithContext(ctx, http.MethodPut, h.RootEndpoint+path, body)
if err != nil {
return &HTTPRequestBuilder{helper: h, request: nil, err: err}
}
req.Header.Set("Content-Type", contentType)
return &HTTPRequestBuilder{
helper: h,
request: req,
err: err,
}
}
func (h *HTTPHelper) dumpRequest(r *http.Request) error {
if h.Dumpster == nil || h.Dumpster == io.Discard {
return nil
}
dump, err := httputil.DumpRequestOut(r, true)
if err != nil {
return fmt.Errorf("error dumping request: %w", err)
}
fmt.Fprintf(h.Dumpster, "%s\n", dump)
return nil
}
func (h *HTTPHelper) dumpResponse(r *http.Response) error {
if h.Dumpster == nil || h.Dumpster == io.Discard {
return nil
}
dump, err := httputil.DumpResponse(r, true)
if err != nil {
return fmt.Errorf("error dumping response: %w", err)
}
fmt.Fprintf(h.Dumpster, "%s\n", dump)
return nil
}
type HTTPRequestBuilder struct {
helper *HTTPHelper
request *http.Request
err error
}
func (rb *HTTPRequestBuilder) AddHeader(key, value string) {
if rb.request == nil {
return
}
rb.request.Header.Add(key, value)
}
func (rb *HTTPRequestBuilder) SetHeader(key, value string) {
if rb.request == nil {
return
}
rb.request.Header.Set(key, value)
}
func (rb *HTTPRequestBuilder) SetBasicAuth() {
if rb.request == nil {
return
}
rb.request.SetBasicAuth(rb.helper.HTTPBasicUsername, "")
}
type RetryOptions struct {
StatusCodes []int
NumRetries uint
RetryDelay time.Duration
}
func (rb *HTTPRequestBuilder) Do() (*http.Response, error) {
return rb.doWithRetries(RetryOptions{})
}
// Expects a response with JSON body to be decoded into `ret`.
func (rb *HTTPRequestBuilder) JSONResDo(ret any) error {
return rb.JSONResDoWithRetries(ret, RetryOptions{})
}
// Expects a response with JSON body to be decoded into `ret`.
func (rb *HTTPRequestBuilder) JSONResDoWithRetries(ret any, retryOpts RetryOptions) error {
res, err := rb.doWithRetries(retryOpts)
if err != nil {
return err
}
defer res.Body.Close()
return rb.parseResponse(res, ret)
}
func (rb *HTTPRequestBuilder) doWithRetries(retryOpts RetryOptions) (*http.Response, error) {
if rb.helper.AccessToken != "" && rb.helper.HTTPBasicUsername != "" {
return nil, fmt.Errorf("cannot set both access token and basic auth")
}
if rb.helper.AccessToken != "" {
rb.AddHeader("Authorization", "Bearer "+rb.helper.AccessToken)
} else if rb.helper.HTTPBasicUsername != "" {
rb.SetBasicAuth()
}
if rb.err != nil {
return nil, rb.err
}
if err := rb.helper.dumpRequest(rb.request); err != nil {
return nil, err
}
res, err := rb.helper.Client.Do(rb.request)
if err != nil {
return nil, fmt.Errorf("error sending request: %w", err)
}
for i := uint(0); i < retryOpts.NumRetries && isIn(res.StatusCode, retryOpts.StatusCodes); i++ {
err = rb.helper.dumpResponse(res)
res.Body.Close()
if err != nil {
return nil, err
}
time.Sleep(retryOpts.RetryDelay)
if res, err = rb.helper.Client.Do(rb.request); err != nil {
return nil, fmt.Errorf("error sending request: %w", err)
}
}
if err := rb.helper.dumpResponse(res); err != nil {
return nil, err
}
return res, nil
}
func (rb *HTTPRequestBuilder) parseResponse(res *http.Response, ret any) error {
dec := json.NewDecoder(res.Body)
if res.StatusCode < 200 || res.StatusCode > 299 {
errpl := new(ApiCallError)
if err := dec.Decode(errpl); err != nil {
return fmt.Errorf("error decoding response: %w", err)
}
return errpl
}
if ret != nil {
if err := dec.Decode(ret); err != nil {
return fmt.Errorf("error decoding response: %w", err)
}
}
return nil
}
// Ideally this would use slices.Contains, but it needs to build with an older go version.
func isIn(code int, codes []int) bool {
for _, c := range codes {
if c == code {
return true
}
}
return false
}
type fileInfo struct {
Name string
TotalChunks int
}
type ExpBackOffOptions struct {
InitialDuration time.Duration
RandomizationFactor float64
Multiplier float64
MaxElapsedTime time.Duration
}
type UploadOptions struct {
BackOffOpts ExpBackOffOptions
ChunkSizeBytes int64
NumWorkers int
}
type FilesUploader struct {
HTTPHelper HTTPHelper
UploadDir string
DumpOut io.Writer
UploadOptions
}
func (u *FilesUploader) Upload(files []string) error {
infos, err := u.getFilesInfos(files)
if err != nil {
return err
}
ctx, cancel := context.WithCancel(context.Background())
// cancel shouldn't be called twice, safeCancel wraps it so that it's safe to do so
safeCancel := func() {
cancel()
cancel = func() {}
}
defer safeCancel()
jobsChan := make(chan uploadChunkJob)
resultsChan := u.startWorkers(ctx, jobsChan)
go func() {
defer close(jobsChan)
u.sendJobs(ctx, jobsChan, infos)
}()
// Only first error will be returned.
var returnErr error
for err := range resultsChan {
if err != nil {
fmt.Fprintf(u.DumpOut, "Error uploading file chunk: %v\n", err)
if returnErr == nil {
returnErr = err
safeCancel()
// Do not return from here and let the cancellation logic propagate, resultsChan
// will be closed eventually.
}
}
}
return returnErr
}
func (u *FilesUploader) getFilesInfos(files []string) ([]fileInfo, error) {
var infos []fileInfo
for _, name := range files {
stat, err := os.Stat(name)
if err != nil {
return nil, err
}
info := fileInfo{
Name: name,
TotalChunks: int((stat.Size() + u.ChunkSizeBytes - 1) / u.ChunkSizeBytes),
}
infos = append(infos, info)
}
return infos, nil
}
func (u *FilesUploader) sendJobs(ctx context.Context, jobsChan chan<- uploadChunkJob, infos []fileInfo) {
for _, info := range infos {
for i := 0; i < info.TotalChunks; i++ {
job := uploadChunkJob{
Filename: info.Name,
ChunkNumber: i + 1,
TotalChunks: info.TotalChunks,
ChunkSizeBytes: u.ChunkSizeBytes,
}
select {
case <-ctx.Done():
return
case jobsChan <- job:
continue
}
}
}
}
func (u *FilesUploader) startWorkers(ctx context.Context, jobsChan <-chan uploadChunkJob) <-chan error {
agg := make(chan error)
wg := sync.WaitGroup{}
for i := 0; i < u.NumWorkers; i++ {
wg.Add(1)
w := uploadChunkWorker{
Context: ctx,
HTTPHelper: u.HTTPHelper,
UploadDir: u.UploadDir,
DumpOut: u.DumpOut,
JobsChan: jobsChan,
UploadOptions: u.UploadOptions,
}
go func() {
defer wg.Done()
ch := w.Start()
for err := range ch {
agg <- err
}
}()
}
go func() {
wg.Wait()
close(agg)
}()
return agg
}
type uploadChunkJob struct {
Filename string
// A number between 1 and `TotalChunks`. The n-th chunk represents a segment of data within the file with size
// `ChunkSizeBytes` starting the `(n-1) * ChunkSizeBytes`-th byte.
ChunkNumber int
TotalChunks int
ChunkSizeBytes int64
}
type uploadChunkWorker struct {
Context context.Context
HTTPHelper HTTPHelper
UploadDir string
DumpOut io.Writer
JobsChan <-chan uploadChunkJob
UploadOptions
}
// Returns a channel that will return the result for each of the handled `uploadChunkJob` instances.
func (w *uploadChunkWorker) Start() <-chan error {
ch := make(chan error)
b := backoff.NewExponentialBackOff()
b.InitialInterval = w.BackOffOpts.InitialDuration
b.RandomizationFactor = w.BackOffOpts.RandomizationFactor
b.Multiplier = w.BackOffOpts.Multiplier
b.MaxElapsedTime = w.BackOffOpts.MaxElapsedTime
b.Reset()
go func() {
defer close(ch)
for job := range w.JobsChan {
var err error
for {
err = w.upload(job)
if err == nil {
b.Reset()
break
}
duration := b.NextBackOff()
if duration == backoff.Stop {
break
} else {
time.Sleep(duration)
}
}
ch <- err
}
}()
return ch
}
func (w *uploadChunkWorker) upload(job uploadChunkJob) error {
ctx, cancel := context.WithCancel(w.Context)
pipeReader, pipeWriter := io.Pipe()
writer := multipart.NewWriter(pipeWriter)
go func() {
defer pipeWriter.Close()
defer writer.Close()
if err := writeMultipartRequest(writer, job); err != nil {
fmt.Fprintf(w.DumpOut, "Error writing multipart request %v", err)
cancel()
}
}()
// client trace to log whether the request's underlying tcp connection was re-used
clientTrace := &httptrace.ClientTrace{
GotConn: func(info httptrace.GotConnInfo) {
if !info.Reused {
const msg = "tcp connection was not reused uploading file chunk: %q," +
"chunk number: %d, chunk total: %d\n"
fmt.Fprintf(w.DumpOut, msg,
filepath.Base(job.Filename), job.ChunkNumber, job.TotalChunks)
}
},
}
traceCtx := httptrace.WithClientTrace(ctx, clientTrace)
res, err := w.HTTPHelper.NewUploadFileRequest(
traceCtx,
"/userartifacts/"+w.UploadDir,
pipeReader,
writer.FormDataContentType()).
Do()
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode != 200 {
const msg = "failed uploading file chunk with status code %q. " +
"File %q, chunk number: %d, chunk total: %d"
return fmt.Errorf(msg, res.Status, filepath.Base(job.Filename), job.ChunkNumber, job.TotalChunks)
}
return nil
}
func writeMultipartRequest(writer *multipart.Writer, job uploadChunkJob) error {
file, err := os.Open(job.Filename)
if err != nil {
return err
}
defer file.Close()
if _, err := file.Seek(int64(job.ChunkNumber-1)*job.ChunkSizeBytes, 0); err != nil {
return err
}
if err := addFormField(writer, "chunk_number", strconv.Itoa(job.ChunkNumber)); err != nil {
return err
}
if err := addFormField(writer, "chunk_total", strconv.Itoa(job.TotalChunks)); err != nil {
return err
}
if err := addFormField(writer, "chunk_size_bytes", strconv.FormatInt(job.ChunkSizeBytes, 10)); err != nil {
return err
}
fw, err := writer.CreateFormFile("file", filepath.Base(job.Filename))
if err != nil {
return err
}
if job.ChunkNumber < job.TotalChunks {
if _, err = io.CopyN(fw, file, job.ChunkSizeBytes); err != nil {
return err
}
} else {
if _, err = io.Copy(fw, file); err != nil {
return err
}
}
return nil
}
func addFormField(writer *multipart.Writer, field, value string) error {
fw, err := writer.CreateFormField(field)
if err != nil {
return err
}
_, err = io.Copy(fw, strings.NewReader(value))
if err != nil {
return err
}
return nil
}