-
Notifications
You must be signed in to change notification settings - Fork 11
/
part.go
385 lines (356 loc) · 9.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
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
373
374
375
376
377
378
379
380
381
382
383
384
385
package getparty
import (
"context"
"fmt"
"io"
"log"
"net/http"
"os"
"strings"
"sync/atomic"
"time"
"github.com/pkg/errors"
"github.com/vbauerster/backoff"
"github.com/vbauerster/backoff/exponential"
"github.com/vbauerster/mpb/v8"
"github.com/vbauerster/mpb/v8/decor"
)
const (
bufSize = 4096
)
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
ctx context.Context
name string
order int
maxTry uint
quiet bool
single bool
totalWriter io.Writer
progress *mpb.Progress
dlogger *log.Logger
}
type flashBar struct {
*mpb.Bar
msgHandler func(message)
prefix string
initialized uint32
}
func (b flashBar) flash(msg string, final bool) {
msg = fmt.Sprintf("%s %s", b.prefix, msg)
b.msgHandler(message{
msg: msg,
final: final,
error: false,
})
}
func (b flashBar) flashErr(msg string, final bool) {
msg = fmt.Sprintf("%s:ERR %s", b.prefix, msg)
b.msgHandler(message{
msg: msg,
final: final,
error: true,
})
}
func (p Part) initBar(fb *flashBar, curTry *uint32) error {
if fb == nil {
return errors.Errorf("expected non nil %T value", fb)
}
if atomic.LoadUint32(&fb.initialized) == 1 {
return nil
}
var barBuilder mpb.BarFillerBuilder
msgCh := make(chan message)
total := p.total()
if total < 0 {
total = 0
barBuilder = mpb.NopStyle()
} else {
barBuilder = mpb.BarStyle().Lbound(" ").Rbound(" ")
}
p.dlogger.Printf("Setting bar total: %d", total)
ctx, cancel := context.WithCancel(p.ctx)
b, err := p.progress.Add(total, barBuilder.Build(), mpb.BarFillerTrim(), mpb.BarPriority(p.order),
mpb.BarOptional(
mpb.BarExtender(mpb.BarFillerFunc(
func(w io.Writer, _ decor.Statistics) error {
_, err := fmt.Fprintln(w)
return err
}), true),
p.single),
mpb.PrependDecorators(
newFlashDecorator(
newMainDecorator(curTry, p.name, "%s %.1f", decor.WCSyncWidthR),
msgCh,
cancel,
16),
decor.Conditional(total == 0,
decor.OnComplete(decor.Spinner([]string{`-`, `\`, `|`, `/`}, decor.WC{W: 2}), "100% "),
decor.OnComplete(decor.NewPercentage("%.2f", decor.WCSyncSpace), "100%")),
),
mpb.AppendDecorators(
decor.OnComplete(
decor.Conditional(total == 0,
decor.Name(""),
decor.NewAverageETA(
decor.ET_STYLE_MMSS,
time.Now(),
decor.FixedIntervalTimeNormalizer(30),
decor.WCSyncWidth,
)), "Avg:"),
decor.AverageSpeed(decor.SizeB1024(0), "%.1f", decor.WCSyncSpace),
decor.OnComplete(decor.Name("", decor.WCSyncSpace), "Peak:"),
newSpeedPeak("%.1f", decor.WCSyncSpace),
),
)
if err != nil {
return err
}
if p.Written != 0 {
p.dlogger.Printf("Setting bar current: %d", p.Written)
b.SetCurrent(p.Written)
p.dlogger.Printf("Setting bar DecoratorAverageAdjust: (now - %s)", p.Elapsed.Truncate(time.Second))
b.DecoratorAverageAdjust(time.Now().Add(-p.Elapsed))
}
fb.Bar = b
fb.prefix = p.name
fb.msgHandler = makeMsgHandler(ctx, msgCh, p.quiet)
atomic.StoreUint32(&fb.initialized, 1)
return nil
}
func (p *Part) download(client *http.Client, req *http.Request, timeout, sleep time.Duration) (err error) {
fpart, err := os.OpenFile(p.FileName, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return errors.WithMessage(err, p.name)
}
defer func() {
if e := fpart.Close(); err == nil {
err = e
}
if p.Skip && err == nil {
p.dlogger.Printf("Removing: %q", fpart.Name())
err = os.Remove(p.FileName)
}
err = errors.WithMessage(err, p.name)
}()
var bar flashBar
var curTry uint32
var statusPartialContent bool
resetTimeout := timeout
prefix := p.dlogger.Prefix()
return backoff.RetryWithContext(p.ctx, exponential.New(exponential.WithBaseDelay(500*time.Millisecond)),
func(attempt uint, reset func()) (retry bool, err error) {
atomic.StoreUint32(&curTry, uint32(attempt))
var totalSleep time.Duration
pWritten := p.Written
start := time.Now()
defer func() {
p.dlogger.Printf("Retry: %v, Error: %v", retry, err)
if p.Skip {
return
}
if n := p.Written - pWritten; n != 0 {
if n >= bufSize {
reset()
timeout = resetTimeout
}
p.Elapsed += time.Since(start) - totalSleep
p.dlogger.Printf("Written: %d", n)
p.dlogger.Printf("Total sleep: %s", totalSleep)
} else if timeout < maxTimeout*time.Second {
timeout += 5 * time.Second
}
if retry && err != nil {
switch attempt {
case 0:
atomic.AddUint32(&globTry, 1)
case p.maxTry:
atomic.AddUint32(&globTry, ^uint32(0))
fmt.Fprintf(p.progress, "%s%s\n", p.dlogger.Prefix(), ErrMaxRetry)
if atomic.LoadUint32(&bar.initialized) == 1 {
go bar.Abort(true)
}
retry, err = false, errors.Wrap(ErrMaxRetry, err.Error())
}
}
}()
req.Header.Set(hRange, p.getRange())
p.dlogger.SetPrefix(fmt.Sprintf(prefix, attempt))
p.dlogger.Printf("GET %q", req.URL)
for k, v := range req.Header {
p.dlogger.Printf("%s: %v", k, v)
}
ctx, cancel := context.WithCancel(p.ctx)
defer cancel()
timer := time.AfterFunc(timeout, func() {
cancel()
msg := "Timeout..."
p.dlogger.Println(msg)
// following check is needed, because this func runs in different goroutine
// at first attempt bar may be not initialized by the time this func runs
if atomic.LoadUint32(&bar.initialized) == 1 {
bar.flash(msg, false)
}
})
defer timer.Stop()
resp, err := client.Do(req.WithContext(ctx))
if err != nil {
if p.Written == 0 {
fmt.Fprintf(p.progress, "%s%s\n", p.dlogger.Prefix(), err.Error())
} else {
err := p.initBar(&bar, &curTry)
if err != nil {
return false, err
}
}
return true, err
}
p.dlogger.Printf("HTTP status: %s", resp.Status)
p.dlogger.Printf("ContentLength: %d", resp.ContentLength)
if jar := client.Jar; jar != nil {
if cookies := 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.StatusPartialContent:
err := p.initBar(&bar, &curTry)
if err != nil {
return false, err
}
if p.Written != 0 {
p.dlogger.Printf("Setting bar refill: %d", p.Written)
bar.SetRefill(p.Written)
}
statusPartialContent = true
case http.StatusOK: // no partial content, download with single part
if statusPartialContent {
panic("http.StatusOK after http.StatusPartialContent")
}
if p.Written == 0 {
if p.order != 1 {
p.Skip = true
p.dlogger.Println("Stopping: no partial content")
return false, nil
}
p.single = true
if resp.ContentLength > 0 {
p.Stop = resp.ContentLength - 1
}
err := p.initBar(&bar, &curTry)
if err != nil {
return false, err
}
} else if atomic.LoadUint32(&bar.initialized) == 1 {
err := fpart.Close()
if err != nil {
panic(err)
}
fpart, err = os.OpenFile(p.FileName, os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
panic(err)
}
p.Written = 0
bar.SetCurrent(0)
} else {
panic(fmt.Sprintf("expected 0 bytes got %d", p.Written))
}
case http.StatusServiceUnavailable:
if atomic.LoadUint32(&bar.initialized) == 1 {
bar.flashErr(cutCode(resp.Status), false)
} else {
fmt.Fprintf(p.progress, "%s%s\n", p.dlogger.Prefix(), cutCode(resp.Status))
}
return true, HttpError{resp.StatusCode, resp.Status}
default:
fmt.Fprintf(p.progress, "%s%s\n", p.dlogger.Prefix(), cutCode(resp.Status))
if atomic.LoadUint32(&bar.initialized) == 1 {
go bar.Abort(true)
atomic.AddUint32(&globTry, ^uint32(0))
}
return false, HttpError{resp.StatusCode, resp.Status}
}
body := bar.ProxyReader(resp.Body)
defer body.Close()
buf := make([]byte, bufSize)
dst := io.MultiWriter(fpart, p.totalWriter)
sleepCtx, sleepCancel := context.WithCancel(p.ctx)
sleepCancel()
for n := 0; err == nil; sleepCancel() {
timer.Reset(timeout)
n, err = io.ReadFull(body, buf)
switch err {
case io.EOF:
if n != 0 {
panic("expected no bytes were read at EOF")
}
continue
case io.ErrUnexpectedEOF:
if n != 0 {
err = nil
} else {
continue
}
default:
if timer.Stop() && sleep != 0 {
sleepCtx, sleepCancel = context.WithTimeout(p.ctx, sleep)
totalSleep += sleep
}
}
_, e := dst.Write(buf[:n])
if e != nil {
panic(e)
}
p.Written += int64(n)
if p.total() <= 0 {
bar.SetTotal(p.Written, false)
}
<-sleepCtx.Done()
}
if err == io.EOF {
if p.total() <= 0 {
p.Stop = p.Written - 1 // so p.isDone() retruns true
bar.EnableTriggerComplete()
}
if p.isDone() {
p.dlogger.Println("Part is done")
return false, nil
} else {
panic("expected part to be done after EOF")
}
}
if p.isDone() {
panic(fmt.Sprintf("expected EOF after part is done, got: %v", err))
}
return p.ctx.Err() == nil, err
})
}
func (p Part) getRange() string {
if p.Stop < 1 {
return "bytes=0-"
}
return fmt.Sprintf("bytes=%d-%d", p.Start+p.Written, p.Stop)
}
func (p Part) total() int64 {
return p.Stop - p.Start + 1
}
func (p Part) isDone() bool {
return p.Written != 0 && p.Written == p.total()
}
func cutCode(err string) string {
_, remainder, found := strings.Cut(err, " ")
if found {
return remainder
}
return err
}