Skip to content

Commit

Permalink
Fix leaking sockets by removing the deferred Close() (closes #34)
Browse files Browse the repository at this point in the history
  • Loading branch information
itsdalmo committed Sep 4, 2018
1 parent ca7559f commit bd68bf9
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions spot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit bd68bf9

Please sign in to comment.