diff --git a/engine/engine.go b/engine/engine.go index dfd39f95..3e5b3817 100644 --- a/engine/engine.go +++ b/engine/engine.go @@ -20,9 +20,6 @@ import ( // purged from the database. const purge = time.Hour * 24 -// defines an empty context. -var nocontext = context.Background() - type engine struct { mu sync.Mutex diff --git a/engine/pinger.go b/engine/pinger.go index 8dd77067..3b279d20 100644 --- a/engine/pinger.go +++ b/engine/pinger.go @@ -67,11 +67,20 @@ func (p *pinger) ping(ctx context.Context, server *autoscaler.Server) error { for i := 0; i < 5; i++ { logger.Debugln("pinging the server") - // TODO should this be moved outside of the loop - timeout, cancel := context.WithTimeout(nocontext, time.Minute) - defer cancel() + timeout, cancel := context.WithTimeout(ctx, time.Minute) + _, err := client.Ping(timeout) + cancel() + + // If the global context is in an error state we + // should assume this is because the program is + // being gracefully terminated. This could cause + // false positive ping errors, so we ignore and + // exit the routine. + if ctx.Err() != nil { + return nil + } - if _, err := client.Ping(timeout); err == nil { + if err == nil { logger.WithField("state", "healthy"). Debugln("server ping successful") return nil