X Tutup
Skip to content

Commit f7a82a2

Browse files
committed
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
1 parent 1d435a3 commit f7a82a2

File tree

2 files changed

+8
-13
lines changed

2 files changed

+8
-13
lines changed

api/cache.go

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,12 @@ func cacheKey(req *http.Request) (string, error) {
6262
return fmt.Sprintf("%x", digest), nil
6363
}
6464

65-
type readCloser struct {
66-
io.Reader
67-
io.Closer
68-
}
69-
7065
func readCache(ttl time.Duration, cacheFile string, req *http.Request) (*http.Response, error) {
7166
f, err := os.Open(cacheFile)
7267
if err != nil {
7368
return nil, err
7469
}
70+
defer f.Close()
7571

7672
fs, err := f.Stat()
7773
if err != nil {
@@ -83,15 +79,13 @@ func readCache(ttl time.Duration, cacheFile string, req *http.Request) (*http.Re
8379
return nil, errors.New("cache expired")
8480
}
8581

86-
res, err := http.ReadResponse(bufio.NewReader(f), req)
87-
if res == nil {
88-
res.Body = &readCloser{
89-
Reader: res.Body,
90-
Closer: f,
91-
}
92-
} else {
93-
f.Close()
82+
body := &bytes.Buffer{}
83+
_, err = io.Copy(body, f)
84+
if err != nil {
85+
return nil, err
9486
}
87+
88+
res, err := http.ReadResponse(bufio.NewReader(body), req)
9589
return res, err
9690
}
9791

api/cache_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ func Test_CacheReponse(t *testing.T) {
3939
if err != nil {
4040
return "", err
4141
}
42+
defer res.Body.Close()
4243
resBody, err := ioutil.ReadAll(res.Body)
4344
if err != nil {
4445
err = fmt.Errorf("ReadAll: %w", err)

0 commit comments

Comments
 (0)
X Tutup