Skip to content

Commit

Permalink
convert Body to our custom nopCloser in ServeHTTPForCache as well, re…
Browse files Browse the repository at this point in the history
…cord RawResponse for cache hits
  • Loading branch information
dencoded committed Nov 28, 2018
1 parent 6bde723 commit 86487a2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
3 changes: 2 additions & 1 deletion mw_redis_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ func (m *RedisCacheMiddleware) ProcessRequest(w http.ResponseWriter, r *http.Req
if err != nil {
log.Error("Could not create response object: ", err)
}
nopCloseResponseBody(newRes)

defer newRes.Body.Close()
for _, h := range hopHeaders {
Expand Down Expand Up @@ -263,7 +264,7 @@ func (m *RedisCacheMiddleware) ProcessRequest(w http.ResponseWriter, r *http.Req

// Record analytics
if !m.Spec.DoNotTrack {
go m.sh.RecordHit(r, 0, newRes.StatusCode, nil)
go m.sh.RecordHit(r, 0, newRes.StatusCode, newRes)
}

// Stop any further execution
Expand Down
11 changes: 6 additions & 5 deletions reverse_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,11 @@ func (p *ReverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) *htt
}

func (p *ReverseProxy) ServeHTTPForCache(rw http.ResponseWriter, req *http.Request) *http.Response {
return p.WrappedServeHTTP(rw, req, true)
resp := p.WrappedServeHTTP(rw, req, true)

nopCloseResponseBody(resp)

return resp
}

func (p *ReverseProxy) CheckHardTimeoutEnforced(spec *APISpec, req *http.Request) (bool, int) {
Expand Down Expand Up @@ -890,11 +894,8 @@ func (n nopCloser) Read(p []byte) (int, error) {
return num, err
}

// Close is a no-op Close plus moves position to the start just in case
// Close is a no-op Close
func (n nopCloser) Close() error {
// seek to the start if body ever called to be closed
n.Seek(0, io.SeekStart)

return nil
}

Expand Down

0 comments on commit 86487a2

Please sign in to comment.