-
Notifications
You must be signed in to change notification settings - Fork 10
/
part.go
277 lines (248 loc) · 6.48 KB
/
part.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
package getparty
import (
"bytes"
"context"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
"sync/atomic"
"time"
"github.com/pkg/errors"
"github.com/vbauerster/backoff"
"github.com/vbauerster/backoff/exponential"
"github.com/vbauerster/mpb/v6"
"github.com/vbauerster/mpb/v6/decor"
)
const (
bufSize = 1 << 12
)
var globTry uint32
// Part represents state of each download part
type Part struct {
FileName string
Start int64
Stop int64
Written int64
Skip bool
Elapsed time.Duration
name string
order int
maxTry int
curTry uint32
single bool
quiet bool
jar http.CookieJar
transport *http.Transport
dlogger *log.Logger
}
func (p *Part) makeBar(progress *mpb.Progress, gate msgGate, total int64) *mpb.Bar {
nlOnComplete := func(w io.Writer, _ int, s decor.Statistics) {
if s.Completed {
fmt.Fprintln(w)
}
}
bar := progress.Add(total,
mpb.NewBarFiller(" =>- "),
mpb.BarFillerTrim(),
mpb.BarPriority(p.order),
mpb.BarOptional(mpb.BarExtender(mpb.BarFillerFunc(nlOnComplete)), p.single),
mpb.PrependDecorators(
newMainDecorator(&p.curTry, "%s %.1f", p.name, gate, decor.WCSyncWidthR),
decor.OnComplete(decor.NewPercentage("%.2f", decor.WCSyncSpace), "100%"),
),
mpb.AppendDecorators(
decor.OnComplete(
decor.NewAverageETA(
decor.ET_STYLE_MMSS,
time.Now(),
decor.FixedIntervalTimeNormalizer(60),
decor.WCSyncWidthR,
),
"Avg:",
),
decor.AverageSpeed(decor.UnitKiB, "%.1f", decor.WCSyncSpace),
decor.OnComplete(decor.Name("", decor.WCSyncSpace), "Peak:"),
newSpeedPeak("%.1f", decor.WCSyncSpace),
),
)
return bar
}
func (p *Part) download(ctx context.Context, progress *mpb.Progress, req *http.Request, timeout uint) (err error) {
defer func() {
err = errors.Wrap(err, p.name)
}()
fpart, err := os.OpenFile(p.FileName, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer func() {
if err := fpart.Close(); err != nil {
p.dlogger.Printf("%q close error: %s", fpart.Name(), err.Error())
}
if p.Skip {
if err := os.Remove(fpart.Name()); err != nil {
p.dlogger.Printf("%q remove error: %s", fpart.Name(), err.Error())
}
}
}()
var bar *mpb.Bar
mg := newMsgGate(p.name, p.quiet)
total := p.Stop - p.Start + 1
initialWritten := p.Written
prefix := p.dlogger.Prefix()
return backoff.Retry(ctx,
exponential.New(exponential.WithBaseDelay(100*time.Millisecond)),
30*time.Second,
func(count int, now time.Time) (retry bool, err error) {
if p.isDone() {
panic("unexpected: done part is engaged")
}
defer func() {
p.Elapsed += time.Since(now)
}()
req.Header.Set(hRange, p.getRange())
p.dlogger.SetPrefix(fmt.Sprintf("%s[%02d] ", prefix, count))
p.dlogger.Printf("GET %q", req.URL)
p.dlogger.Printf("%s: %s", hUserAgentKey, req.Header.Get(hUserAgentKey))
p.dlogger.Printf("%s: %s", hRange, req.Header.Get(hRange))
ctxTimeout := time.Duration(timeout) * time.Second
if count > 0 {
ctxTimeout = time.Duration((1<<uint(count-1))*timeout) * time.Second
if bound := 10 * time.Minute; ctxTimeout > bound {
ctxTimeout = bound
}
atomic.AddUint32(&globTry, 1)
atomic.StoreUint32(&p.curTry, uint32(count))
mg.flash(&message{msg: "Retrying..."})
}
ctx, cancel := context.WithCancel(ctx)
defer cancel()
timer := time.AfterFunc(ctxTimeout, func() {
msg := "Timeout..."
mg.flash(&message{msg: msg})
p.dlogger.Print(msg)
cancel()
})
defer timer.Stop()
client := &http.Client{
Transport: p.transport,
Jar: p.jar,
}
resp, err := client.Do(req.WithContext(ctx))
if err != nil {
return true, err
}
p.dlogger.Printf("resp.Status: %s", resp.Status)
p.dlogger.Printf("resp.ContentLength: %d", resp.ContentLength)
if cookies := p.jar.Cookies(req.URL); len(cookies) != 0 {
p.dlogger.Println("CookieJar:")
for _, cookie := range cookies {
p.dlogger.Printf(" %q", cookie)
}
}
switch resp.StatusCode {
case http.StatusOK: // no partial content, so download with single part
if p.order != 0 {
p.Skip = true
p.dlogger.Print("no partial content, skipping...")
return false, nil
}
total = resp.ContentLength
p.Stop = resp.ContentLength - 1
p.Written = 0
p.single = true
case http.StatusForbidden, http.StatusTooManyRequests:
flushed := make(chan struct{})
mg.flash(&message{
msg: resp.Status,
final: true,
done: flushed,
})
<-flushed
fallthrough
default:
if resp.StatusCode != http.StatusPartialContent {
if bar != nil {
bar.Abort(false)
}
return false, errors.Errorf("unexpected status: %s", resp.Status)
}
if p.single && resp.ContentLength > 0 {
total = resp.ContentLength
p.Stop = resp.ContentLength - 1
}
}
if bar == nil {
bar = p.makeBar(progress, mg, total)
p.dlogger.Printf("bar total: %d", total)
}
body := bar.ProxyReader(resp.Body)
defer body.Close()
if p.Written > 0 {
p.dlogger.Printf("bar refill written: %d", p.Written)
bar.SetRefill(p.Written)
if p.Written-initialWritten == 0 {
bar.DecoratorAverageAdjust(time.Now().Add(-p.Elapsed))
bar.IncrInt64(p.Written)
}
}
pWrittenSnap := p.Written
buf, max := bytes.NewBuffer(make([]byte, 0, bufSize)), int64(bufSize)
var n int64
for timer.Reset(ctxTimeout) {
n, err = io.CopyN(buf, body, max)
if err != nil {
p.dlogger.Printf("CopyN err: %s", err.Error())
if e, ok := err.(*url.Error); ok {
mg.flash(&message{
msg: fmt.Sprintf("%.30s..", e.Err.Error()),
})
if e.Temporary() {
max -= n
continue
}
}
break
}
n, _ = io.Copy(fpart, buf)
p.Written += n
if total <= 0 {
bar.SetTotal(p.Written+max*2, false)
}
max = bufSize
}
n, _ = io.Copy(fpart, buf)
p.Written += n
p.dlogger.Printf("written: %d", p.Written-pWrittenSnap)
if total <= 0 {
p.Stop = p.Written - 1
}
if err == io.EOF {
return false, nil
}
if count == p.maxTry {
flushed := make(chan struct{})
mg.flash(&message{
msg: "max retry!",
final: true,
done: flushed,
})
<-flushed
bar.Abort(false)
return false, ErrMaxRetry
}
return true, err
})
}
func (p Part) getRange() string {
if p.Stop <= 0 {
return "bytes=0-"
}
return fmt.Sprintf("bytes=%d-%d", p.Start+p.Written, p.Stop)
}
func (p Part) isDone() bool {
return p.Skip || (p.Written > 0 && p.Written > p.Stop-p.Start)
}