Skip to content

Commit

Permalink
app/vmagent/remotewrite: add missing resp.Body.Close() after pushin…
Browse files Browse the repository at this point in the history
…g data to remote storage

Missing body close could disable HTTP keep-alive connections.

Updates #653
  • Loading branch information
valyala committed Jul 28, 2020
1 parent 79c30cf commit 62ed38c
Showing 1 changed file with 28 additions and 23 deletions.
51 changes: 28 additions & 23 deletions app/vmagent/remotewrite/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,28 +220,33 @@ again:
goto again
}
statusCode := resp.StatusCode
if statusCode/100 != 2 {
metrics.GetOrCreateCounter(fmt.Sprintf(`vmagent_remotewrite_requests_total{url=%q, status_code="%d"}`, c.urlLabelValue, statusCode)).Inc()
retryDuration *= 2
if retryDuration > time.Minute {
retryDuration = time.Minute
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
logger.Errorf("cannot read response body from %q: %s", c.remoteWriteURL, err)
} else {
logger.Errorf("unexpected status code received after sending a block with size %d bytes to %q: %d; response body=%q; re-sending the block in %.3f seconds",
len(block), c.remoteWriteURL, statusCode, body, retryDuration.Seconds())
}
t := time.NewTimer(retryDuration)
select {
case <-c.stopCh:
t.Stop()
return
case <-t.C:
}
c.retriesCount.Inc()
goto again
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()
retryDuration *= 2
if retryDuration > time.Minute {
retryDuration = time.Minute
}
body, err := ioutil.ReadAll(resp.Body)
_ = resp.Body.Close()
if err != nil {
logger.Errorf("cannot read response body from %q: %s", c.remoteWriteURL, err)
} else {
logger.Errorf("unexpected status code received after sending a block with size %d bytes to %q: %d; response body=%q; re-sending the block in %.3f seconds",
len(block), c.remoteWriteURL, statusCode, body, retryDuration.Seconds())
}
t := time.NewTimer(retryDuration)
select {
case <-c.stopCh:
t.Stop()
return
case <-t.C:
}
c.requestsOKCount.Inc()
c.retriesCount.Inc()
goto again
}

0 comments on commit 62ed38c

Please sign in to comment.