-
Notifications
You must be signed in to change notification settings - Fork 10
/
part.go
276 lines (248 loc) · 6.4 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
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/v5"
"github.com/vbauerster/mpb/v5/decor"
)
const (
bufSize = 1 << 12
)
var (
ErrGiveUp = errors.New("give up!")
ErrNilBody = errors.New("nil body")
)
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
quiet bool
dlogger *log.Logger
transport *http.Transport
}
func (p *Part) makeBar(total int64, progress *mpb.Progress, gate msgGate) *mpb.Bar {
bar := progress.AddBar(total,
mpb.TrimSpace(),
mpb.BarStyle(" =>- "),
mpb.BarPriority(p.order),
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) {
var bar *mpb.Bar
defer func() {
if err != nil {
if bar != nil && !p.isDone() && !p.quiet {
bar.Abort(false)
}
err = errors.WithMessage(err, p.name)
}
p.dlogger.Printf("quit: %v", err)
}()
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())
}
}
}()
total := p.Stop - p.Start + 1
mg := newMsgGate(p.name, p.quiet)
bar = p.makeBar(total, progress, mg)
initialWritten := p.Written
prefix := p.dlogger.Prefix()
err = backoff.Retry(ctx,
exponential.New(exponential.WithBaseDelay(50*time.Millisecond)),
time.Minute,
func(count int, now time.Time) (retry bool, err error) {
if count > p.maxTry {
return false, ErrGiveUp
}
if p.isDone() {
p.dlogger.Println("done in try, quitting...")
return false, nil
}
p.dlogger.SetPrefix(fmt.Sprintf("%s[%02d] ", prefix, count))
req.Header.Set(hRange, p.getRange())
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))
defer func() {
p.Elapsed += time.Since(now)
}()
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..."})
} else {
bar.DecoratorAverageAdjust(now)
}
p.dlogger.Printf("ctxTimeout: %s", ctxTimeout)
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}
resp, err := client.Do(req.WithContext(ctx))
if err != nil {
p.dlogger.Printf("client do: %s", err.Error())
return true, err
}
p.dlogger.Printf("resp.Status: %s", resp.Status)
p.dlogger.Printf("resp.ContentLength: %d", resp.ContentLength)
switch resp.StatusCode {
case http.StatusOK: // no partial content, so download with single part
if p.order > 0 {
p.Skip = true
bar.Abort(true)
p.dlogger.Print("no partial content, skipping...")
return false, nil
}
total = resp.ContentLength
bar.SetTotal(total, false)
p.Stop = total - 1
p.dlogger.Printf("resetting written: %d", p.Written)
p.Written = 0
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 {
return false, errors.Errorf("unexpected status: %s", resp.Status)
}
}
body := resp.Body
if !p.quiet {
body = bar.ProxyReader(resp.Body)
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)
}
}
} else {
bar.Abort(true)
}
if body == nil {
return false, ErrNilBody
}
defer body.Close()
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 && !p.quiet {
bar.SetTotal(p.Written+max*2, false)
}
max = bufSize
}
n, _ = io.Copy(fpart, buf)
p.Written += n
p.dlogger.Printf("total written: %d", p.Written-pWrittenSnap)
if total <= 0 {
p.Stop = p.Written - 1
}
if err == io.EOF {
return false, nil
}
return !p.isDone(), err
})
if err == ErrGiveUp {
flushed := make(chan struct{})
mg.flash(&message{
msg: err.Error(),
final: true,
done: flushed,
})
<-flushed
}
return 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 > p.Stop-p.Start
}