forked from cuducos/chunk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
222 lines (200 loc) · 6.65 KB
/
main.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
package main
import (
"bytes"
"context"
"fmt"
"log"
"net/http"
"os"
"path/filepath"
"sync"
"time"
"github.com/avast/retry-go"
)
const (
DefaultTimeoutPerChunk = 90 * time.Second
DefaultMaxParallelDownloadsPerServer = 8
DefaultMaxRetriesPerChunk = 5
DefaultChunkSize = 8192
DefaultWaitBetweenRetries = 0 * time.Minute
)
// DownloadStatus is the data propagated via the channel sent back to the user
// and it contains information about the download from each URL.
type DownloadStatus struct {
// URL this status refers to
URL string
// DownloadedFilePath in the user local system
DownloadedFilePath string
// FileSizeBytes is the total size of the file as informed by the server
FileSizeBytes uint64
// DownloadedFileBytes already downloaded from this URL
DownloadedFileBytes uint64
// Any non-recoerable error captured during the download (this means that
// some errors are ignored the download is retried instead of propagating
// the error).
Error error
}
// IsFinished informs the user whether a download is done (successfully or
// with error).
func (s *DownloadStatus) IsFinished() bool {
return s.Error != nil || s.DownloadedFileBytes == s.FileSizeBytes
}
// Downloader can be configured by the user before starting the download using
// the following fields. This configurations impacts how the download will be
// handled, including retries, amoutn of requets, and size of each request, for
// example.
type Downloader struct {
// Client is the HTTP client used for every request needed to download all
// the files.
client *http.Client
// TimeoutPerChunk is the timeout for the download of each chunk from each
// URL. A chunk is a part of a file requested using the content range HTTP
// header. Thus, this timeout is not the timeout for the each file or for
// the the download of every file).
TimeoutPerChunk time.Duration
// MaxParallelDownloadsPerServer controls how many requests are sent in
// parallel to the same server. If all the URLs are from the same server
// this is the total of parallel requests. If the user is downloading files
// from different servers (including different subdomains), this limit is
// applied to each server idependently.
MaxParallelDownloadsPerServer uint
// MaxRetriesPerChunk is the maximum amount of retries for each HTTP request
// using the content range header that fails.
MaxRetriesPerChunk uint
// ChunkSize is the maximum size of each HTTP request done using the
// content range header. There is no way to specify how many chunks a
// download will need, the focus is on slicing it in smaller chunks so slow
// and unstable servers can respond before dropping it.
ChunkSize uint64
// WaitBetweenRetries is an optional pause before retrying an HTTP request
// that has failed.
WaitBetweenRetries time.Duration
}
func (d *Downloader) downloadFileWithContext(ctx context.Context, u string) ([]byte, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u, nil)
if err != nil {
return nil, fmt.Errorf("error creating the request for %s: %w", u, err)
}
resp, err := d.client.Do(req)
if err != nil {
return nil, fmt.Errorf("error sending a get http request to %s: %w", u, err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("got http response %s from %s: %w", resp.Status, u, err)
}
var b bytes.Buffer
_, err = b.ReadFrom(resp.Body)
if err != nil {
return nil, fmt.Errorf("error reading body from %s: %w", u, err)
}
return b.Bytes(), nil
}
func (d *Downloader) downloadFileWithTimeout(userCtx context.Context, u string) ([]byte, error) {
ctx, cancel := context.WithTimeout(userCtx, d.TimeoutPerChunk) // need to propagate context, which might contain app-specific data.
defer cancel()
ch := make(chan []byte)
errs := make(chan error)
go func() {
b, err := d.downloadFileWithContext(ctx, u)
if err != nil {
errs <- err
return
}
ch <- b
}()
select {
case <-userCtx.Done():
cancel()
return nil, userCtx.Err()
case <-ctx.Done():
return nil, fmt.Errorf("request to %s ended due to timeout: %w", u, ctx.Err())
case err := <-errs:
return nil, fmt.Errorf("request to %s failed: %w", u, err)
case b := <-ch:
return b, nil
}
}
func (d *Downloader) downloadFile(ctx context.Context, u string) ([]byte, error) {
ch := make(chan []byte, 1)
defer close(ch)
err := retry.Do(
func() error {
b, err := d.downloadFileWithTimeout(ctx, u)
if err != nil {
return err
}
ch <- b
return nil
},
retry.Attempts(d.MaxRetriesPerChunk),
retry.MaxDelay(d.WaitBetweenRetries),
)
if err != nil {
return nil, fmt.Errorf("error downloading %s: %w", u, err)
}
b := <-ch
return b, nil
}
// DownloadWithContext is a version of Download that takes a context. The
// context can be used to stop all downloads in progress.
func (d *Downloader) DownloadWithContext(ctx context.Context, urls ...string) <-chan DownloadStatus {
if d.client == nil {
d.client = &http.Client{Timeout: d.TimeoutPerChunk}
}
ch := make(chan DownloadStatus)
var wg sync.WaitGroup
for _, u := range urls {
wg.Add(1)
go func(u string) {
defer wg.Done()
s := DownloadStatus{URL: u}
defer func() { ch <- s }()
s.DownloadedFilePath = filepath.Join(os.TempDir(), filepath.Base(u))
b, err := d.downloadFile(ctx, u)
if err != nil {
s.Error = err
return
}
if err := os.WriteFile(s.DownloadedFilePath, b, 0655); err != nil {
s.Error = err
return
}
s.DownloadedFileBytes = uint64(len(b))
s.FileSizeBytes = uint64(len(b))
}(u)
}
go func() {
wg.Wait()
close(ch)
}()
return ch
}
// Download from all URLs slicing each in a series of chunks, of small HTTP
// requests using the content range header.
func (d *Downloader) Download(urls ...string) <-chan DownloadStatus {
return d.DownloadWithContext(context.Background(), urls...)
}
// NewDownloader creates a downloader with the defalt configuration. Check
// the constants in this package for their values.
func DefaultDownloader() *Downloader {
return &Downloader{
TimeoutPerChunk: DefaultTimeoutPerChunk,
MaxParallelDownloadsPerServer: DefaultMaxParallelDownloadsPerServer,
MaxRetriesPerChunk: DefaultMaxRetriesPerChunk,
ChunkSize: DefaultChunkSize,
WaitBetweenRetries: DefaultWaitBetweenRetries,
}
}
func main() {
d := DefaultDownloader()
for s := range d.Download(os.Args[1:len(os.Args)]...) {
if s.Error != nil {
log.Fatal(s.Error)
}
if s.IsFinished() {
log.Printf("Downloaded %s (%d bytes) to %s (%d bytes).", s.URL, s.FileSizeBytes, s.DownloadedFilePath, s.DownloadedFileBytes)
}
}
log.Println("All download(s) finished successfully.")
}