Skip to content

Commit

Permalink
Cancel the archive request when the context is canceled (#20)
Browse files Browse the repository at this point in the history
Previously it was possible for the request to go forever unless there
was an explicit failure in any of the `.Write()` calls.

This change makes it such that context deadlines can now cause the
request to exit early if possible.
  • Loading branch information
lhchavez authored Dec 12, 2021
1 parent eb969ae commit 32b0379
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,11 +552,28 @@ func handleArchive(
}
defer tree.Free()

select {
case <-ctx.Done():
return errors.Wrap(
ctx.Err(),
"context cancelled",
)
default:
}

w.Header().Set("Content-Type", contentType)
z := zip.NewWriter(w)
defer z.Close()

err = tree.Walk(func(parent string, entry *git.TreeEntry) error {
select {
case <-ctx.Done():
return errors.Wrap(
ctx.Err(),
"context cancelled",
)
default:
}
fullPath := path.Join(parent, entry.Name)
if entry.Type == git.ObjectTree {
_, err := z.CreateHeader(&zip.FileHeader{
Expand Down

0 comments on commit 32b0379

Please sign in to comment.