-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathclient.go
More file actions
346 lines (262 loc) · 11 KB
/
client.go
File metadata and controls
346 lines (262 loc) · 11 KB
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
package nicehttp
import (
"errors"
"fmt"
"github.com/lithdew/bytesutil"
"github.com/valyala/fasthttp"
"golang.org/x/sync/errgroup"
"io"
"os"
"runtime"
"time"
)
// zeroTime is the zero-value of time.Time.
var zeroTime time.Time
// Transport represents the interface of a HTTP client supported by nicehttp.
type Transport interface {
Do(req *fasthttp.Request, res *fasthttp.Response) error
DoTimeout(req *fasthttp.Request, res *fasthttp.Response, timeout time.Duration) error
DoDeadline(req *fasthttp.Request, res *fasthttp.Response, deadline time.Time) error
}
// Client wraps over fasthttp.Client a couple of useful helper functions.
type Client struct {
// The underlying instance which nicehttp.Client wraps around.
Instance Transport
// Decide whether or not URLs that accept being downloaded in parallel chunks are handled with multiple workers.
AcceptsRanges bool
// The number of workers that are to be spawned for downloading chunks in parallel.
NumWorkers int
// Size of individual byte chunks downloaded.
ChunkSize int
// Max number of redirects to follow before a request is marked to have failed.
MaxRedirectCount int
}
// NewClient instantiates a new nicehttp.Client with sane configuration defaults.
func NewClient() Client {
return WrapClient(new(fasthttp.Client))
}
// WrapClient wraps an existing fasthttp.Client or Transport into a nicehttp.Client.
func WrapClient(instance Transport) Client {
return Client{
// Instantiate an empty fasthttp.Client.
Instance: instance,
// Allow for parallel chunk-based downloading.
AcceptsRanges: true,
// Default to the number of available CPUs.
NumWorkers: runtime.NumCPU(),
// 10 MiB chunks.
ChunkSize: 10 * 1024 * 1024,
// Redirect 16 times at most.
MaxRedirectCount: 16,
}
}
// Do sends a HTTP request prescribed in req and populates its results into res. It additionally handles redirects
// unlike the de-facto Do(req, res) method in fasthttp.
func (c *Client) Do(req *fasthttp.Request, res *fasthttp.Response) error {
return c.DoDeadline(req, res, zeroTime)
}
// DoTimeout sends a HTTP request prescribed in req and populates its results into res. It additionally handles
// redirects unlike the de-facto Do(req, res) method in fasthttp. It overrides the default timeout set.
func (c *Client) DoTimeout(req *fasthttp.Request, res *fasthttp.Response, timeout time.Duration) error {
return c.DoDeadline(req, res, time.Now().Add(timeout))
}
// DoDeadline sends a HTTP request prescribed in req and populates its results into res. It additionally handles
// redirects unlike the de-facto Do(req, res) method in fasthttp. It overrides the default timeout set with a deadline.
func (c *Client) DoDeadline(req *fasthttp.Request, res *fasthttp.Response, deadline time.Time) error {
for i := 0; i <= c.MaxRedirectCount; i++ {
var err error
if deadline.IsZero() {
err = c.Instance.Do(req, res)
} else {
err = c.Instance.DoDeadline(req, res, deadline)
}
if err != nil {
return err
}
if !fasthttp.StatusCodeIsRedirect(res.StatusCode()) {
return nil
}
location := res.Header.Peek("Location")
if len(location) == 0 {
return errors.New("missing 'Location' header after redirect")
}
req.URI().UpdateBytes(location)
res.Reset()
}
return errors.New("redirected too many times")
}
// QueryHeaders learns from url its content length, and if it accepts parallel chunk fetching.
func (c *Client) QueryHeaders(url string) (contentLength int, acceptsRanges bool) {
return c.QueryHeadersDeadline(url, zeroTime)
}
// QueryHeadersTimeout learns from url its content length, and if it accepts parallel chunk fetching.
func (c *Client) QueryHeadersTimeout(url string, timeout time.Duration) (contentLength int, acceptsRanges bool) {
return c.QueryHeadersDeadline(url, time.Now().Add(timeout))
}
// QueryHeadersDeadline learns from url its content length, and if it accepts parallel chunk fetching.
func (c *Client) QueryHeadersDeadline(url string, deadline time.Time) (contentLength int, acceptsRanges bool) {
req := fasthttp.AcquireRequest()
defer fasthttp.ReleaseRequest(req)
res := fasthttp.AcquireResponse()
defer fasthttp.ReleaseResponse(res)
req.Header.SetMethod(fasthttp.MethodHead)
req.SetRequestURI(url)
if err := c.DoDeadline(req, res, deadline); err == nil {
if contentLength = res.Header.ContentLength(); contentLength <= 0 {
contentLength = 0
}
acceptsRanges = bytesutil.String(res.Header.Peek("Accept-Ranges")) == "bytes"
}
return contentLength, acceptsRanges
}
// Download downloads the contents of url and writes its contents to w.
func (c *Client) Download(w Writer, url string, contentLength int, acceptsRanges bool) error {
return c.DownloadDeadline(w, url, contentLength, acceptsRanges, zeroTime)
}
// DownloadTimeout downloads the contents of url and writes its contents to w.
func (c *Client) DownloadTimeout(w Writer, url string, contentLength int, acceptsRanges bool, timeout time.Duration) error {
return c.DownloadDeadline(w, url, contentLength, acceptsRanges, time.Now().Add(timeout))
}
// DownloadDeadline downloads the contents of url and writes its contents to w.
func (c *Client) DownloadDeadline(w Writer, url string, contentLength int, acceptsRanges bool, deadline time.Time) error {
if c.AcceptsRanges && acceptsRanges {
if contentLength <= 0 {
return fmt.Errorf("content length is %d - see doc for (*fasthttp.ResponseHeader).ContentLength()", contentLength)
}
if err := c.DownloadInChunksDeadline(w, url, contentLength, deadline); err != nil {
return err
}
return nil
}
if err := c.DownloadSeriallyDeadline(w, url, deadline); err != nil {
return err
}
return nil
}
// DownloadBytes downloads the contents of url, and returns them as a byte slice.
func (c *Client) DownloadBytes(dst []byte, url string) ([]byte, error) {
return c.DownloadBytesDeadline(dst, url, zeroTime)
}
// DownloadBytesTimeout downloads the contents of url, and returns them as a byte slice.
func (c *Client) DownloadBytesTimeout(dst []byte, url string, timeout time.Duration) ([]byte, error) {
return c.DownloadBytesDeadline(dst, url, time.Now().Add(timeout))
}
// DownloadBytesDeadline downloads the contents of url, and returns them as a byte slice.
func (c *Client) DownloadBytesDeadline(dst []byte, url string, deadline time.Time) ([]byte, error) {
contentLength, acceptsRanges := c.QueryHeadersDeadline(url, deadline)
w := NewWriteBuffer(bytesutil.ExtendSlice(dst, contentLength))
if err := c.DownloadDeadline(w, url, contentLength, acceptsRanges, deadline); err != nil {
return w.dst, err
}
return w.dst, nil
}
// DownloadFile downloads the contents of url, and writes its contents to a newly-created file titled filename.
func (c *Client) DownloadFile(filename, url string) error {
return c.DownloadFileDeadline(filename, url, zeroTime)
}
// DownloadFileTimeout downloads the contents of url, and writes its contents to a newly-created file titled filename.
func (c *Client) DownloadFileTimeout(filename, url string, timeout time.Duration) error {
return c.DownloadFileDeadline(filename, url, time.Now().Add(timeout))
}
// DownloadFileDeadline downloads the contents of url, and writes its contents to a newly-created file titled filename.
func (c *Client) DownloadFileDeadline(filename, url string, deadline time.Time) error {
contentLength, acceptsRanges := c.QueryHeadersDeadline(url, deadline)
w, err := os.Create(filename)
if err != nil {
return fmt.Errorf("failed to open dest file: %w", err)
}
if err := w.Truncate(int64(contentLength)); err != nil {
return fmt.Errorf("failed to truncate file to %d byte(s): %w", contentLength, err)
}
return c.DownloadDeadline(w, url, contentLength, acceptsRanges, deadline)
}
// DownloadSerially serially downloads the contents of url and writes it to w.
func (c *Client) DownloadSerially(w io.Writer, url string) error {
return c.DownloadSeriallyDeadline(w, url, zeroTime)
}
// DownloadSeriallyTimeout serially downloads the contents of url and writes it to w.
func (c *Client) DownloadSeriallyTimeout(w io.Writer, url string, timeout time.Duration) error {
return c.DownloadSeriallyDeadline(w, url, time.Now().Add(timeout))
}
// DownloadSeriallyDeadline serially downloads the contents of url and writes it to w.
func (c *Client) DownloadSeriallyDeadline(w io.Writer, url string, deadline time.Time) error {
req := fasthttp.AcquireRequest()
defer fasthttp.ReleaseRequest(req)
res := fasthttp.AcquireResponse()
defer fasthttp.ReleaseResponse(res)
req.SetRequestURI(url)
if err := c.DoDeadline(req, res, deadline); err != nil {
return fmt.Errorf("failed to download %q: %w", url, err)
}
return res.BodyWriteTo(w)
}
// DownloadInChunks downloads file at url comprised of length bytes in chunks using multiple workers, and stores it in
// writer w.
func (c *Client) DownloadInChunks(f io.WriterAt, url string, length int) error {
return c.DownloadInChunksDeadline(f, url, length, zeroTime)
}
// DownloadInChunksTimeout downloads file at url comprised of length bytes in chunks using multiple workers, and stores
// it in writer w.
func (c *Client) DownloadInChunksTimeout(f io.WriterAt, url string, length int, timeout time.Duration) error {
return c.DownloadInChunksDeadline(f, url, length, time.Now().Add(timeout))
}
// DownloadInChunksDeadline downloads file at url comprised of length bytes in chunks using multiple workers, and
// stores it in writer w.
func (c *Client) DownloadInChunksDeadline(f io.WriterAt, url string, length int, deadline time.Time) error {
timeout := (<-chan time.Time)(nil)
if t := -time.Since(deadline); t > 0 {
timer := fasthttp.AcquireTimer(t)
defer fasthttp.ReleaseTimer(timer)
timeout = timer.C
}
var g errgroup.Group
// ByteRange represents a byte range.
type ByteRange struct{ Start, End int }
ch := make(chan ByteRange, c.NumWorkers)
// Spawn w workers that will dispatch and execute byte range-inclusive HTTP requests.
for i := 0; i < c.NumWorkers; i++ {
i := i
g.Go(func() error {
req := fasthttp.AcquireRequest()
defer fasthttp.ReleaseRequest(req)
res := fasthttp.AcquireResponse()
defer fasthttp.ReleaseResponse(res)
req.SetRequestURI(url)
for r := range ch {
req.Header.SetByteRange(r.Start, r.End)
if err := c.DoDeadline(req, res, deadline); err != nil {
return fmt.Errorf("worker %d failed to get bytes range (start: %d, end: %d): %w", i, r.Start, r.End, err)
}
if err := res.BodyWriteTo(NewWriterAtOffset(f, int64(r.Start))); err != nil {
return fmt.Errorf("worker %d failed to write to file at offset %d: %w", i, r.Start, err)
}
}
return nil
})
}
// Fill up ch with byte ranges to be download from url.
var r ByteRange
Feed:
for r.End < length {
r.End += c.ChunkSize
if r.End > length {
r.End = length
}
select {
case ch <- r:
case <-timeout:
break Feed
}
r.Start += c.ChunkSize
if r.Start > length {
r.Start = length
}
}
close(ch)
// Wait until all byte ranges have been downloaded, or return early if an error was encountered downloading
// a chunk.
if err := g.Wait(); err != nil {
return fmt.Errorf("failed to download %q in chunks: %w", url, err)
}
return nil
}