-
Notifications
You must be signed in to change notification settings - Fork 0
/
parallel_download.go
301 lines (280 loc) · 6.35 KB
/
parallel_download.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
package req
import (
"context"
"crypto/md5"
"encoding/hex"
"fmt"
"io"
"math"
urlpkg "net/url"
"os"
"path/filepath"
"strings"
"sync"
)
type ParallelDownload struct {
url string
client *Client
concurrency int
output io.Writer
filename string
segmentSize int64
perm os.FileMode
tempRootDir string
tempDir string
taskCh chan *downloadTask
doneCh chan struct{}
wgDoneCh chan struct{}
errCh chan error
wg sync.WaitGroup
taskMap map[int]*downloadTask
taskNotifyCh chan *downloadTask
mu sync.Mutex
lastIndex int
}
func (pd *ParallelDownload) completeTask(task *downloadTask) {
pd.mu.Lock()
pd.taskMap[task.index] = task
pd.mu.Unlock()
go func() {
select {
case pd.taskNotifyCh <- task:
case <-pd.doneCh:
}
}()
}
func (pd *ParallelDownload) popTask(index int) *downloadTask {
pd.mu.Lock()
if task, ok := pd.taskMap[index]; ok {
delete(pd.taskMap, index)
pd.mu.Unlock()
return task
}
pd.mu.Unlock()
for {
task := <-pd.taskNotifyCh
if task.index == index {
pd.mu.Lock()
delete(pd.taskMap, index)
pd.mu.Unlock()
return task
}
}
}
func md5Sum(s string) string {
sum := md5.Sum([]byte(s))
return hex.EncodeToString(sum[:])
}
func (pd *ParallelDownload) ensure() error {
if pd.concurrency <= 0 {
pd.concurrency = 5
}
if pd.segmentSize <= 0 {
pd.segmentSize = 1073741824 // 10MB
}
if pd.perm == 0 {
pd.perm = 0777
}
if pd.tempRootDir == "" {
pd.tempRootDir = os.TempDir()
}
pd.tempDir = filepath.Join(pd.tempRootDir, md5Sum(pd.url))
if pd.client.DebugLog {
pd.client.log.Debugf("use temporary directory %s", pd.tempDir)
pd.client.log.Debugf("download with %d concurrency and %d bytes segment size", pd.concurrency, pd.segmentSize)
}
err := os.MkdirAll(pd.tempDir, os.ModePerm)
if err != nil {
return err
}
pd.taskCh = make(chan *downloadTask)
pd.doneCh = make(chan struct{})
pd.wgDoneCh = make(chan struct{})
pd.errCh = make(chan error)
pd.taskMap = make(map[int]*downloadTask)
pd.taskNotifyCh = make(chan *downloadTask)
return nil
}
func (pd *ParallelDownload) SetSegmentSize(segmentSize int64) *ParallelDownload {
pd.segmentSize = segmentSize
return pd
}
func (pd *ParallelDownload) SetTempRootDir(tempRootDir string) *ParallelDownload {
pd.tempRootDir = tempRootDir
return pd
}
func (pd *ParallelDownload) SetFileMode(perm os.FileMode) *ParallelDownload {
pd.perm = perm
return pd
}
func (pd *ParallelDownload) SetConcurrency(concurrency int) *ParallelDownload {
pd.concurrency = concurrency
return pd
}
func (pd *ParallelDownload) SetOutput(output io.Writer) *ParallelDownload {
if output != nil {
pd.output = output
}
return pd
}
func (pd *ParallelDownload) SetOutputFile(filename string) *ParallelDownload {
pd.filename = filename
return pd
}
func getRangeTempFile(rangeStart, rangeEnd int64, workerDir string) string {
return filepath.Join(workerDir, fmt.Sprintf("temp-%d-%d", rangeStart, rangeEnd))
}
type downloadTask struct {
index int
rangeStart, rangeEnd int64
tempFilename string
tempFile *os.File
}
func (pd *ParallelDownload) handleTask(t *downloadTask, ctx ...context.Context) {
pd.wg.Add(1)
defer pd.wg.Done()
t.tempFilename = getRangeTempFile(t.rangeStart, t.rangeEnd, pd.tempDir)
if pd.client.DebugLog {
pd.client.log.Debugf("downloading segment %d-%d", t.rangeStart, t.rangeEnd)
}
file, err := os.OpenFile(t.tempFilename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
if err != nil {
pd.errCh <- err
return
}
err = pd.client.Get(pd.url).
SetHeader("Range", fmt.Sprintf("bytes=%d-%d", t.rangeStart, t.rangeEnd)).
SetOutput(file).
Do(ctx...).Err
if err != nil {
pd.errCh <- err
return
}
t.tempFile = file
pd.completeTask(t)
}
func (pd *ParallelDownload) startWorker(ctx ...context.Context) {
for {
select {
case t := <-pd.taskCh:
pd.handleTask(t, ctx...)
case <-pd.doneCh:
return
}
}
}
func (pd *ParallelDownload) mergeFile() {
defer pd.wg.Done()
file, err := pd.getOutputFile()
if err != nil {
pd.errCh <- err
return
}
for i := 0; ; i++ {
task := pd.popTask(i)
tempFile, err := os.Open(task.tempFilename)
if err != nil {
pd.errCh <- err
return
}
_, err = io.Copy(file, tempFile)
tempFile.Close()
if err != nil {
pd.errCh <- err
return
}
if i < pd.lastIndex {
continue
}
break
}
if pd.client.DebugLog {
pd.client.log.Debugf("removing temporary directory %s", pd.tempDir)
}
err = os.RemoveAll(pd.tempDir)
if err != nil {
pd.errCh <- err
}
}
func (pd *ParallelDownload) Do(ctx ...context.Context) error {
err := pd.ensure()
if err != nil {
return err
}
for i := 0; i < pd.concurrency; i++ {
go pd.startWorker(ctx...)
}
resp := pd.client.Head(pd.url).Do(ctx...)
if resp.Err != nil {
return resp.Err
}
if resp.ContentLength <= 0 {
return fmt.Errorf("bad content length: %d", resp.ContentLength)
}
pd.lastIndex = int(math.Ceil(float64(resp.ContentLength)/float64(pd.segmentSize))) - 1
pd.wg.Add(1)
go pd.mergeFile()
go func() {
pd.wg.Wait()
close(pd.wgDoneCh)
}()
totalBytes := resp.ContentLength
start := int64(0)
for i := 0; ; i++ {
end := start + (pd.segmentSize - 1)
if end > (totalBytes - 1) {
end = totalBytes - 1
}
task := &downloadTask{
index: i,
rangeStart: start,
rangeEnd: end,
}
pd.taskCh <- task
if end < (totalBytes - 1) {
start = end + 1
continue
}
break
}
select {
case <-pd.wgDoneCh:
if pd.client.DebugLog {
if pd.filename != "" {
pd.client.log.Debugf("download completed from %s to %s", pd.url, pd.filename)
} else {
pd.client.log.Debugf("download completed for %s", pd.url)
}
}
close(pd.doneCh)
case err := <-pd.errCh:
return err
}
return nil
}
func (pd *ParallelDownload) getOutputFile() (io.Writer, error) {
outputFile := pd.output
if outputFile != nil {
return outputFile, nil
}
if pd.filename == "" {
u, err := urlpkg.Parse(pd.url)
if err != nil {
panic(err)
}
paths := strings.Split(u.Path, "/")
for i := len(paths) - 1; i > 0; i-- {
if paths[i] != "" {
pd.filename = paths[i]
break
}
}
if pd.filename == "" {
pd.filename = "download"
}
}
if pd.client.outputDirectory != "" && !filepath.IsAbs(pd.filename) {
pd.filename = filepath.Join(pd.client.outputDirectory, pd.filename)
}
return os.OpenFile(pd.filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, pd.perm)
}