Avoid error from cache file being prematurely closed
I have no idea what's going on there, so I'll just give up the streaming approach and read the entire contents of the cache file to memory. https://github.com/cli/cli/pull/2035/checks?check_run_id=1194798056
This commit is contained in:
parent
1d435a3e2e
commit
f7a82a216b
2 changed files with 8 additions and 13 deletions
20
api/cache.go
20
api/cache.go
|
|
@ -62,16 +62,12 @@ func cacheKey(req *http.Request) (string, error) {
|
|||
return fmt.Sprintf("%x", digest), nil
|
||||
}
|
||||
|
||||
type readCloser struct {
|
||||
io.Reader
|
||||
io.Closer
|
||||
}
|
||||
|
||||
func readCache(ttl time.Duration, cacheFile string, req *http.Request) (*http.Response, error) {
|
||||
f, err := os.Open(cacheFile)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
fs, err := f.Stat()
|
||||
if err != nil {
|
||||
|
|
@ -83,15 +79,13 @@ func readCache(ttl time.Duration, cacheFile string, req *http.Request) (*http.Re
|
|||
return nil, errors.New("cache expired")
|
||||
}
|
||||
|
||||
res, err := http.ReadResponse(bufio.NewReader(f), req)
|
||||
if res == nil {
|
||||
res.Body = &readCloser{
|
||||
Reader: res.Body,
|
||||
Closer: f,
|
||||
}
|
||||
} else {
|
||||
f.Close()
|
||||
body := &bytes.Buffer{}
|
||||
_, err = io.Copy(body, f)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
res, err := http.ReadResponse(bufio.NewReader(body), req)
|
||||
return res, err
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -39,6 +39,7 @@ func Test_CacheReponse(t *testing.T) {
|
|||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
resBody, err := ioutil.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("ReadAll: %w", err)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue