-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgobble.go
More file actions
372 lines (304 loc) · 8.18 KB
/
gobble.go
File metadata and controls
372 lines (304 loc) · 8.18 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
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
package main
import (
"context"
"flag"
"fmt"
"io"
"math/rand"
"net/http"
"os"
"os/signal"
"strconv"
"sync"
"syscall"
"time"
)
const (
defaultParallelism = 16
contentLengthHeader = "Content-Length"
rangeHeader = "Range"
acceptRangesHeader = "Accept-Ranges"
)
func closeFile(f *os.File) {
err := f.Close()
if err != nil {
panic(err)
}
}
type DownloadJob struct {
begin int
end int
part int
}
type FileStats struct {
fileSize int
isParallelSupported bool
maxParallelism int
}
func NewProgressTracker(totalSize int64) *ProgressTracker {
pt := &ProgressTracker{
totalSize: totalSize,
done: make(chan struct{}),
}
go pt.Run()
return pt
}
// Exponential backoff with jitter
func backoff(attempt int) {
base := time.Duration(1<<attempt) * time.Second
jitter := time.Duration(rand.Intn(1000)) * time.Millisecond
sleep := base + jitter
fmt.Printf("⏳ Backing off for %v...\n", sleep)
time.Sleep(sleep)
}
func detectMaxConnections(url string) int {
low, high := 1, defaultParallelism
maxConns := 1
attempts := 1
for low <= high {
mid := low + (high-low)/2
fmt.Printf("🔍 Testing %d connections...\n", mid)
ok, rateLimited := testParallelRequest(url, mid)
if rateLimited {
fmt.Println("⚠️ Rate-limited (429). Backing off...")
backoff(attempts)
attempts++
high = mid - 1
continue
}
attempts = 1 // reset attempts on success or non-rate-limit failure
if ok {
maxConns = mid
low = mid + 1
} else {
high = mid - 1
}
}
fmt.Printf("✅ Max safe parallel connections: %d\n", maxConns)
return maxConns
}
func testParallelRequest(url string, maxConns int) (ok bool, rateLimited bool) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
wg := sync.WaitGroup{}
wg.Add(maxConns)
errChan := make(chan int, maxConns)
for i := 0; i < maxConns; i++ {
go func(i int) {
defer wg.Done()
start := int64(i * 100)
end := start + 99
req, _ := http.NewRequestWithContext(ctx, "GET", url, nil)
req.Header.Set("Range", fmt.Sprintf("bytes=%d-%d", start, end))
resp, err := http.DefaultClient.Do(req)
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
errChan <- 500 // generic server error
return
}
if resp.StatusCode == http.StatusTooManyRequests {
errChan <- 429
return
}
if resp.StatusCode != http.StatusPartialContent {
errChan <- resp.StatusCode
return
}
}(i)
}
wg.Wait()
close(errChan)
for code := range errChan {
if code == 429 {
return false, true // failed due to rate limit
}
if code != 0 {
return false, false // failed for another reason
}
}
return true, false
}
func downloadPart(ctx context.Context, url string, output string, job DownloadJob, wg *sync.WaitGroup, tracker *ProgressTracker) {
defer wg.Done()
req, _ := http.NewRequestWithContext(ctx, "GET", url, nil)
req.Header.Set(rangeHeader, fmt.Sprintf("bytes=%d-%d", job.begin, job.end))
res, err := http.DefaultClient.Do(req)
if err != nil {
if ctx.Err() != nil {
fmt.Printf("🚫 Part %d download canceled due to context cancellation.\n", job.part)
return
}
panic(err)
}
defer res.Body.Close()
partFile, err := os.OpenFile(output, os.O_WRONLY, 0666)
if err != nil {
fmt.Printf("Error opening file: %v\n", err)
return
}
defer closeFile(partFile)
partFile.Seek(int64(job.begin), 0)
// _, err = io.Copy(partFile, res.Body) // TODO check if using zero copy internally
// if err != nil {
// panic(err)
// }
buf := make([]byte, 32*1024) // 32KB buffer
for {
n, err := res.Body.Read(buf)
if n > 0 {
_, wErr := partFile.Write(buf[:n])
if wErr != nil {
fmt.Printf("Error writing to file: %v\n", wErr)
return
}
tracker.Add(int64(n))
}
if err == io.EOF {
break
}
if err != nil {
// Return without printing if the error is due to context cancellation
if ctx.Err() != nil {
return
}
fmt.Printf("Error reading from body: %v\n", err)
return
}
}
}
func download(ctx context.Context, url string, output string, stats FileStats, tracker *ProgressTracker) error {
var jobs []DownloadJob
partSize := stats.fileSize / stats.maxParallelism
for i := 0; i < stats.maxParallelism; i++ {
begin := i * partSize
end := begin + partSize - 1
if i == stats.maxParallelism-1 { // last chunk
end = stats.fileSize - 1
}
jobs = append(jobs, DownloadJob{begin, end, i + 1})
}
var wg sync.WaitGroup
for _, job := range jobs {
wg.Add(1)
go downloadPart(ctx, url, output, job, &wg, tracker)
}
wg.Wait()
tracker.Finish()
return nil
}
func getStatsFallback(url string) FileStats {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
panic(err) // It is a possibility that the URL is malformed
}
req.Header.Set(rangeHeader, "bytes=0-1")
resp, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
fileSize, _ := strconv.Atoi(resp.Header.Get(contentLengthHeader))
fmt.Printf("Status code: %d\n", resp.StatusCode)
if resp.StatusCode != http.StatusPartialContent {
fmt.Println("Server does not support range requests. Using 1 thread for download")
return FileStats{
fileSize: fileSize,
maxParallelism: 1,
isParallelSupported: false,
}
} else {
fmt.Printf("Server supports range requests. Using %d threads for download\n", defaultParallelism)
return FileStats{
fileSize: fileSize,
maxParallelism: defaultParallelism,
isParallelSupported: true,
}
}
}
func getStats(url string) FileStats {
/* Note: Some webservers may not have HEAD configured and may outright reject it
If this happens, fallback on determining if parallelism is supported using Get request
*/
var fileSize int
isParallelSupported := false
maxParallelism := 1
resp, err := http.Head(url)
fileSize, _ = strconv.Atoi(resp.Header.Get(contentLengthHeader))
fmt.Println("HEAD Status code:", resp.StatusCode)
if err != nil || resp.StatusCode != http.StatusOK {
// check with a get request with range headers
return getStatsFallback(url)
}
/*
resp.Body is a network stream (an open TCP connection + buffers)
1. Resource cleanup - Prevent "too many open files"
2. Connection reuse
3. Proper memory release - buffers allocated for the response wont be freed until GC runs
*/
defer resp.Body.Close()
if resp.Header.Get(acceptRangesHeader) == "bytes" {
isParallelSupported = true
maxParallelism = detectMaxConnections(url)
}
return FileStats{
fileSize: fileSize,
maxParallelism: maxParallelism,
isParallelSupported: isParallelSupported,
}
}
// Create the file for storing the download content
func prepareOutputFile(fileName string, fileSize int) {
out, err := os.Create(fileName)
if err != nil {
panic(err)
}
defer closeFile(out)
// Guarantee the downloaded file has the correct size upfront and makes parallel writes safe.
out.Truncate(int64(fileSize))
}
func main() {
start := time.Now()
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
link := flag.String("url", "", "URL to download")
filename := flag.String("o", "", "filename")
flag.Parse()
if *link == "" {
fmt.Println("Please provide a URL with -url")
flag.PrintDefaults()
os.Exit(1)
}
downloadFile, err := buildDownloadPath(*filename, *link)
if err != nil {
panic(err)
}
stats := getStats(*link)
fmt.Printf("File size: %d MB, Parallelism: %d\n", stats.fileSize/1024/1024, stats.maxParallelism)
prepareOutputFile(downloadFile, stats.fileSize)
tracker := NewProgressTracker(int64(stats.fileSize))
doneChan := make(chan error, 1)
go func() {
doneChan <- download(ctx, *link, downloadFile, stats, tracker)
}()
select {
case err := <-doneChan:
if err != nil {
fmt.Printf("Download failed: %v\n", err)
} else {
duration := time.Since(start)
fmt.Printf("Total time taken %s\n", duration)
}
case <-sigChan:
fmt.Println("\n❗ Received interrupt. Exiting...")
cancel()
<-doneChan
fmt.Println("Download cancelled. Cleaning up partial file.")
os.Remove(downloadFile)
duration := time.Since(start)
fmt.Printf("Total time taken before interruption: %s\n", duration)
}
}