Fix concurrent r/w to heartbeat time

This commit is contained in:
Alex Dadgar 2016-02-09 22:43:16 -08:00
parent 913f98f738
commit 071216a730

View file

@ -90,6 +90,7 @@ type Client struct {
lastHeartbeat time.Time lastHeartbeat time.Time
heartbeatTTL time.Duration heartbeatTTL time.Duration
heartbeatLock sync.Mutex
// allocs is the current set of allocations // allocs is the current set of allocations
allocs map[string]*AllocRunner allocs map[string]*AllocRunner
@ -647,7 +648,9 @@ func (c *Client) run() {
if err := c.updateNodeStatus(); err != nil { if err := c.updateNodeStatus(); err != nil {
heartbeat = time.After(c.retryIntv(registerRetryIntv)) heartbeat = time.After(c.retryIntv(registerRetryIntv))
} else { } else {
c.heartbeatLock.Lock()
heartbeat = time.After(c.heartbeatTTL) heartbeat = time.After(c.heartbeatTTL)
c.heartbeatLock.Unlock()
} }
case <-c.shutdownCh: case <-c.shutdownCh:
@ -711,6 +714,9 @@ func (c *Client) registerNode() error {
if len(resp.EvalIDs) != 0 { if len(resp.EvalIDs) != 0 {
c.logger.Printf("[DEBUG] client: %d evaluations triggered by node registration", len(resp.EvalIDs)) c.logger.Printf("[DEBUG] client: %d evaluations triggered by node registration", len(resp.EvalIDs))
} }
c.heartbeatLock.Lock()
defer c.heartbeatLock.Unlock()
c.lastHeartbeat = time.Now() c.lastHeartbeat = time.Now()
c.heartbeatTTL = resp.HeartbeatTTL c.heartbeatTTL = resp.HeartbeatTTL
return nil return nil
@ -736,6 +742,9 @@ func (c *Client) updateNodeStatus() error {
if resp.Index != 0 { if resp.Index != 0 {
c.logger.Printf("[DEBUG] client: state updated to %s", req.Status) c.logger.Printf("[DEBUG] client: state updated to %s", req.Status)
} }
c.heartbeatLock.Lock()
defer c.heartbeatLock.Unlock()
c.lastHeartbeat = time.Now() c.lastHeartbeat = time.Now()
c.heartbeatTTL = resp.HeartbeatTTL c.heartbeatTTL = resp.HeartbeatTTL
return nil return nil