app/vmagent/remotewrite: add missing resp.Body.Close() after pushing data to remote storage

Missing body close could disable HTTP keep-alive connections.

Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/653
This commit is contained in:
Aliaksandr Valialkin 2020-07-28 20:52:00 +03:00
parent 0f6f0d30d3
commit baebe86844

View file

@ -220,13 +220,20 @@ again:
goto again goto again
} }
statusCode := resp.StatusCode statusCode := resp.StatusCode
if statusCode/100 != 2 { if statusCode/100 == 2 {
_ = resp.Body.Close()
c.requestsOKCount.Inc()
return
}
// Unexpected status code returned
metrics.GetOrCreateCounter(fmt.Sprintf(`vmagent_remotewrite_requests_total{url=%q, status_code="%d"}`, c.urlLabelValue, statusCode)).Inc() metrics.GetOrCreateCounter(fmt.Sprintf(`vmagent_remotewrite_requests_total{url=%q, status_code="%d"}`, c.urlLabelValue, statusCode)).Inc()
retryDuration *= 2 retryDuration *= 2
if retryDuration > time.Minute { if retryDuration > time.Minute {
retryDuration = time.Minute retryDuration = time.Minute
} }
body, err := ioutil.ReadAll(resp.Body) body, err := ioutil.ReadAll(resp.Body)
_ = resp.Body.Close()
if err != nil { if err != nil {
logger.Errorf("cannot read response body from %q: %s", c.remoteWriteURL, err) logger.Errorf("cannot read response body from %q: %s", c.remoteWriteURL, err)
} else { } else {
@ -243,5 +250,3 @@ again:
c.retriesCount.Inc() c.retriesCount.Inc()
goto again goto again
} }
c.requestsOKCount.Inc()
}