@@ -47,6 +47,7 @@ type chunk struct {
47
47
End int64
48
48
Downloaded int64
49
49
50
+ failed bool
50
51
retryTimes int
51
52
}
52
53
@@ -265,10 +266,6 @@ func (f *Fetcher) Wait() (err error) {
265
266
return <- f .doneCh
266
267
}
267
268
268
- type fetchResult struct {
269
- err error
270
- }
271
-
272
269
func (f * Fetcher ) fetch () {
273
270
var ctx context.Context
274
271
ctx , f .cancel = context .WithCancel (context .Background ())
@@ -312,22 +309,35 @@ func (f *Fetcher) fetch() {
312
309
313
310
func (f * Fetcher ) fetchChunk (index int , ctx context.Context ) (err error ) {
314
311
chunk := f .chunks [index ]
312
+ chunk .failed = false
315
313
chunk .retryTimes = 0
316
314
317
315
var (
318
- client = f .buildClient ()
319
- buf = make ([]byte , 8192 )
320
- maxRetries = 3
316
+ client = f .buildClient ()
317
+ buf = make ([]byte , 8192 )
321
318
)
322
319
// retry until all remain chunks failed
323
320
for {
324
321
// if chunk is completed, return
325
322
if f .meta .Res .Range && chunk .Downloaded >= chunk .End - chunk .Begin + 1 {
326
323
return nil
327
324
}
328
-
329
- if chunk .retryTimes >= maxRetries {
330
- return
325
+ // if all chunks failed, return
326
+ if chunk .failed {
327
+ allFailed := true
328
+ for _ , c := range f .chunks {
329
+ if ! c .failed {
330
+ allFailed = false
331
+ break
332
+ }
333
+ }
334
+ if allFailed {
335
+ if chunk .retryTimes >= 3 {
336
+ return
337
+ } else {
338
+ chunk .retryTimes ++
339
+ }
340
+ }
331
341
}
332
342
333
343
var (
@@ -378,18 +388,10 @@ func (f *Fetcher) fetchChunk(index int, ctx context.Context) (err error) {
378
388
return
379
389
}
380
390
// retry request after 1 second
381
- chunk .retryTimes = chunk . retryTimes + 1
391
+ chunk .failed = true
382
392
time .Sleep (time .Second )
383
393
continue
384
394
}
385
- // if chunk is completed, reset other not complete chunks retry times
386
- if err == nil {
387
- for _ , c := range f .chunks {
388
- if c != chunk && c .retryTimes > 0 {
389
- c .retryTimes = 0
390
- }
391
- }
392
- }
393
395
break
394
396
}
395
397
return
0 commit comments