Skip to content

Commit

Permalink
ignore ping errors when global context in error state
Browse files Browse the repository at this point in the history
  • Loading branch information
bradrydzewski committed Mar 26, 2022
1 parent 4547d56 commit 8cd545f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
3 changes: 0 additions & 3 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
17 changes: 13 additions & 4 deletions engine/pinger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8cd545f

Please sign in to comment.