Close HTTP connections on HTTP errors too.

Move defer resp.Body.Close() up to make sure it's called even when the
HTTP request returns something other than 200 or Decoder construction
fails. This avoids leaking and eventually running out of file descriptors.
This commit is contained in:
Anders Daljord Morken 2015-09-10 22:32:40 +02:00
parent 66f376f75a
commit 9fb65a91af

View file

@ -437,6 +437,8 @@ func (t *Target) scrape(appender storage.SampleAppender) (err error) {
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("server returned HTTP status %s", resp.Status)
}
@ -445,7 +447,6 @@ func (t *Target) scrape(appender storage.SampleAppender) (err error) {
if err != nil {
return err
}
defer resp.Body.Close()
sdec := expfmt.SampleDecoder{
Dec: dec,