diff --git a/spot.go b/spot.go index 7b2848d..ced0185 100644 --- a/spot.go +++ b/spot.go @@ -35,19 +35,21 @@ func pollSpotTermination(ctx context.Context) chan time.Time { log.WithError(err).Info("Failed to query metadata service") continue } - defer res.Body.Close() - - // will return 200 OK with termination notice - if res.StatusCode != http.StatusOK { - continue - } + // We read the body immediately so that we can close the body in one place + // and still use 'continue' if any of our conditions are false. body, err := ioutil.ReadAll(res.Body) + res.Body.Close() if err != nil { log.WithError(err).Info("Failed to read response from metadata service") continue } + // will return 200 OK with termination notice + if res.StatusCode != http.StatusOK { + continue + } + // if 200 OK, expect a body like 2015-01-05T18:02:00Z t, err := time.Parse(terminationTimeFormat, string(body)) if err != nil {