From 9dcebcd8a3103f43636a4e13d4e6a02feebbc30b Mon Sep 17 00:00:00 2001 From: Mahmood Ali Date: Fri, 19 Apr 2019 09:12:50 -0400 Subject: [PATCH] client: avoid registering node twice right away I noticed that `watchNodeUpdates()` almost immediately after `registerAndHeartbeat()` calls `retryRegisterNode()`, well after 5 seconds. This call is unnecessary and made debugging a bit harder. So here, we ensure that we only re-register node for new node events, not for initial registration. --- client/client.go | 11 +++-------- client/util.go | 11 +++++++++++ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/client/client.go b/client/client.go index ca2a81a5a..4cdbd3da6 100644 --- a/client/client.go +++ b/client/client.go @@ -1468,13 +1468,7 @@ func (c *Client) watchNodeEvents() { // batchEvents stores events that have yet to be published var batchEvents []*structs.NodeEvent - // Create and drain the timer - timer := time.NewTimer(0) - timer.Stop() - select { - case <-timer.C: - default: - } + timer := stoppedTimer() defer timer.Stop() for { @@ -1930,7 +1924,8 @@ func (c *Client) updateNodeLocked() { // it will update the client node copy and re-register the node. func (c *Client) watchNodeUpdates() { var hasChanged bool - timer := time.NewTimer(c.retryIntv(nodeUpdateRetryIntv)) + + timer := stoppedTimer() defer timer.Stop() for { diff --git a/client/util.go b/client/util.go index e3662a303..af3bd7540 100644 --- a/client/util.go +++ b/client/util.go @@ -3,6 +3,7 @@ package client import ( "fmt" "math/rand" + "time" "github.com/hashicorp/nomad/nomad/structs" ) @@ -63,3 +64,13 @@ func shuffleStrings(list []string) { list[i], list[j] = list[j], list[i] } } + +// stoppedTimer returns a timer that's stopped and wouldn't fire until +// it's reset +func stoppedTimer() *time.Timer { + timer := time.NewTimer(0) + if !timer.Stop() { + <-timer.C + } + return timer +}