From 9c8ca9fd8d221af7f23495d5bcd716c3258ff85a Mon Sep 17 00:00:00 2001 From: Greg Jones Date: Sun, 5 Feb 2017 13:12:21 +0100 Subject: [PATCH] Fix request-map not being cleared up after requests Fixes #62 --- httpcache.go | 12 ++++++------ httpcache_test.go | 5 +++++ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/httpcache.go b/httpcache.go index 69842a7..ee6bf5c 100644 --- a/httpcache.go +++ b/httpcache.go @@ -223,22 +223,22 @@ func (t *Transport) RoundTrip(req *http.Request) (resp *http.Response, err error } if req2 != nil { // Associate original request with cloned request so we can refer to - // it in CancelRequest() + // it in CancelRequest(). Release the mapping when it's no longer needed. t.setModReq(req, req2) - req = req2 - defer func() { + defer func(originalReq *http.Request) { // Release req/clone mapping on error if err != nil { - t.setModReq(req, nil) + t.setModReq(originalReq, nil) } if resp != nil { // Release req/clone mapping on body close/EOF resp.Body = &onEOFReader{ rc: resp.Body, - fn: func() { t.setModReq(req, nil) }, + fn: func() { t.setModReq(originalReq, nil) }, } } - }() + }(req) + req = req2 } } } diff --git a/httpcache_test.go b/httpcache_test.go index 3fbe5c8..0ef7bab 100644 --- a/httpcache_test.go +++ b/httpcache_test.go @@ -465,6 +465,11 @@ func TestGetWithLastModified(t *testing.T) { if err != nil { t.Fatal(err) } + defer func() { + if len(s.transport.modReq) != 0 { + t.Errorf("Request-map is not empty") + } + }() { resp, err := s.client.Do(req) if err != nil {