Skip to content

Commit

Permalink
Avoid setting a body recorder for healtcheck req/resp (#72)
Browse files Browse the repository at this point in the history
Currently, the code sends a healthcheck request, then setup a recorder
for the request and response body, which does not work obviously.

This also seems to be causing a leak inside golang http code.
Removing the unnecessary code fixes the leak.
  • Loading branch information
vadmeste authored Nov 24, 2022
1 parent a4c3168 commit d9d2417
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 13 deletions.
14 changes: 2 additions & 12 deletions http-tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func (r *recordRequest) Data() []byte {
return BodyPlaceHolder
}

func httpInternalTrace(req *http.Request, resp *http.Response, reqTime, respTime time.Time, backend *Backend) {
func traceHealthCheckReq(req *http.Request, resp *http.Response, reqTime, respTime time.Time, backend *Backend) {
ti := InternalTrace(req, resp, reqTime, respTime)
doTrace(ti, backend)
}
Expand All @@ -197,13 +197,6 @@ func InternalTrace(req *http.Request, resp *http.Response, reqTime, respTime tim
for _, enc := range req.TransferEncoding {
reqHeaders.Add("Transfer-Encoding", enc)
}
reqBodyRecorder := &recordRequest{Reader: req.Body, logBody: false, headers: reqHeaders}
req.Body = ioutil.NopCloser(reqBodyRecorder)
respBodyStr := "<BODY>"
if resp.Body != nil {
respBody, _ := ioutil.ReadAll(resp.Body)
respBodyStr = string(respBody)
}

rq := traceRequestInfo{
Time: reqTime,
Expand All @@ -212,13 +205,11 @@ func InternalTrace(req *http.Request, resp *http.Response, reqTime, respTime tim
RawQuery: req.URL.RawQuery,
Client: getSourceIP(req),
Headers: reqHeaders,
Body: string(reqBodyRecorder.Data()),
}
rs := traceResponseInfo{
Time: respTime,
Headers: resp.Header.Clone(),
StatusCode: resp.StatusCode,
Body: respBodyStr,
}
if rs.StatusCode == 0 {
rs.StatusCode = http.StatusOK
Expand All @@ -229,9 +220,8 @@ func InternalTrace(req *http.Request, resp *http.Response, reqTime, respTime tim

t.CallStats = traceCallStats{
Latency: rs.Time.Sub(rq.Time),
Rx: reqBodyRecorder.Size(),
Tx: len(rs.Body),
}

t.Type = TraceMsgType
if globalDebugEnabled {
t.Type = DebugMsgType
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ func (b *Backend) healthCheck() {
}
if globalTrace != "application" {
if resp != nil {
httpInternalTrace(req, resp, reqTime, respTime, b)
traceHealthCheckReq(req, resp, reqTime, respTime, b)
}
}
time.Sleep(b.healthCheckDuration)
Expand Down

0 comments on commit d9d2417

Please sign in to comment.