Skip to content

Commit 8cd545f

Browse files
committed
ignore ping errors when global context in error state
1 parent 4547d56 commit 8cd545f

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

engine/engine.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ import (
2020
// purged from the database.
2121
const purge = time.Hour * 24
2222

23-
// defines an empty context.
24-
var nocontext = context.Background()
25-
2623
type engine struct {
2724
mu sync.Mutex
2825

engine/pinger.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,20 @@ func (p *pinger) ping(ctx context.Context, server *autoscaler.Server) error {
6767
for i := 0; i < 5; i++ {
6868
logger.Debugln("pinging the server")
6969

70-
// TODO should this be moved outside of the loop
71-
timeout, cancel := context.WithTimeout(nocontext, time.Minute)
72-
defer cancel()
70+
timeout, cancel := context.WithTimeout(ctx, time.Minute)
71+
_, err := client.Ping(timeout)
72+
cancel()
73+
74+
// If the global context is in an error state we
75+
// should assume this is because the program is
76+
// being gracefully terminated. This could cause
77+
// false positive ping errors, so we ignore and
78+
// exit the routine.
79+
if ctx.Err() != nil {
80+
return nil
81+
}
7382

74-
if _, err := client.Ping(timeout); err == nil {
83+
if err == nil {
7584
logger.WithField("state", "healthy").
7685
Debugln("server ping successful")
7786
return nil

0 commit comments

Comments
 (0)