Skip to content

Commit

Permalink
Merge pull request #35 from itsdalmo/fix-leaking-sockets
Browse files Browse the repository at this point in the history
Fix leaking sockets by removing the deferred Close() (closes #34)
  • Loading branch information
lox authored Sep 6, 2018
2 parents 4532ace + bd68bf9 commit a158fa3
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 a158fa3

Please sign in to comment.