Skip to content

Commit

Permalink
fix: fail fast on cancel
Browse files Browse the repository at this point in the history
  • Loading branch information
monkeyWie committed Dec 23, 2024
1 parent 6cb81e3 commit 351c47b
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions internal/protocol/http/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,25 +266,30 @@ func (f *Fetcher) fetch() {
for i := 0; i < len(f.chunks); i++ {
i := i
f.eg.Go(func() error {
err := f.fetchChunk(i, ctx)
// if canceled, fail fast
if errors.Is(err, context.Canceled) {
return err
}
fr := &fetchResult{
err: f.fetchChunk(i, ctx),
err: err,
}
fetchResults[i] = fr
return nil
})
}

go func() {
f.eg.Wait()
var err error
err := f.eg.Wait()
// error returned only if canceled, just return
if err != nil {
return
}
// check all fetch results, if any error, return
for _, fr := range fetchResults {
// check if canceled
if errors.Is(fr.err, context.Canceled) {
return
}
// return first error
if err == nil && fr.err != nil {
if fr.err != nil {
err = fr.err
break
}
}

Expand Down

0 comments on commit 351c47b

Please sign in to comment.