Skip to content
This repository has been archived by the owner on Apr 20, 2023. It is now read-only.

Fix unclosed response bodies that are not being used #102

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions httpcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,15 @@ func (t *Transport) RoundTrip(req *http.Request) (resp *http.Response, err error
for _, header := range endToEndHeaders {
cachedResp.Header[header] = resp.Header[header]
}
resp.Body.Close()
Copy link

@CAFxX CAFxX Feb 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would be ideal to do

io.Copy(io.Discard, resp.Body)
resp.Body.Close()

so that the HTTP connection can also be reused

(same below)

resp = cachedResp
} else if (err != nil || (cachedResp != nil && resp.StatusCode >= 500)) &&
req.Method == "GET" && canStaleOnError(cachedResp.Header, req.Header) {
// In case of transport failure and stale-if-error activated, returns cached content
// when available
if resp != nil && resp.Body != nil {
resp.Body.Close()
}
return cachedResp, nil
} else {
if err != nil || resp.StatusCode != http.StatusOK {
Expand Down