Skip to content

Commit

Permalink
Check for Nil response (PHNX-3021) (#2)
Browse files Browse the repository at this point in the history
Motivation
---
Without checking for a nil value for the response from a client.Do call, we run the risk of attempting to reference a property of a null object which would cause our program to hard crash.

This is based off my own learning of Go and the following reference:
https://medium.com/@KeithAlpichi/go-gotcha-closing-a-nil-http-response-body-with-defer-9b7a3eb30e8c

I may be off base; in which case, we can ignore this PR.

Modification
---
- Check the response from our client call and return err if the resp is null

https://centeredge.atlassian.net/browse/PHNX-3021
  • Loading branch information
gbakerce authored and centeredgebot[bot] committed Aug 8, 2019
1 parent 9a57e72 commit 04b8642
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions notifier.go
Expand Up @@ -46,16 +46,19 @@ func notifyStateChange(info *monitorInfo, newStatus bool) error {
for i := 0; i < retryAttempts; i++ {
client := &http.Client{}
resp, err := client.Do(req)
defer resp.Body.Close()
if resp != nil {

log.WithFields(log.Fields{
"svc": info.ServiceName,
"pod": info.PodName,
"ns": info.Namespace,
}).Debug("Notification result ", resp.Status)
defer resp.Body.Close()

if err == nil {
return nil
log.WithFields(log.Fields{
"svc": info.ServiceName,
"pod": info.PodName,
"ns": info.Namespace,
}).Debug("Notification result ", resp.Status)

if err == nil {
return nil
}
}

time.Sleep(retryInterval)
Expand Down

0 comments on commit 04b8642

Please sign in to comment.