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.
pull/1070/head
Anders Daljord Morken 10 years ago
parent 66f376f75a
commit 9fb65a91af
  1. 3
      retrieval/target.go

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

Loading…
Cancel
Save