Skip to content

Commit d02018f

Browse files
authored
Merge pull request gregjones#63 from gregjones/fix-map-cleanup
Fix request-map not being cleared up after requests
2 parents 4137817 + 9c8ca9f commit d02018f

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

‎httpcache.go‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -223,22 +223,22 @@ func (t *Transport) RoundTrip(req *http.Request) (resp *http.Response, err error
223223
}
224224
if req2 != nil {
225225
// Associate original request with cloned request so we can refer to
226-
// it in CancelRequest()
226+
// it in CancelRequest(). Release the mapping when it's no longer needed.
227227
t.setModReq(req, req2)
228-
req = req2
229-
defer func() {
228+
defer func(originalReq *http.Request) {
230229
// Release req/clone mapping on error
231230
if err != nil {
232-
t.setModReq(req, nil)
231+
t.setModReq(originalReq, nil)
233232
}
234233
if resp != nil {
235234
// Release req/clone mapping on body close/EOF
236235
resp.Body = &onEOFReader{
237236
rc: resp.Body,
238-
fn: func() { t.setModReq(req, nil) },
237+
fn: func() { t.setModReq(originalReq, nil) },
239238
}
240239
}
241-
}()
240+
}(req)
241+
req = req2
242242
}
243243
}
244244
}

‎httpcache_test.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,11 @@ func TestGetWithLastModified(t *testing.T) {
465465
if err != nil {
466466
t.Fatal(err)
467467
}
468+
defer func() {
469+
if len(s.transport.modReq) != 0 {
470+
t.Errorf("Request-map is not empty")
471+
}
472+
}()
468473
{
469474
resp, err := s.client.Do(req)
470475
if err != nil {

0 commit comments

Comments
 (0)