use time ticker, update how network resources are compared

This commit is contained in:
Chelsea Holland Komlo 2018-02-26 17:02:15 -05:00
parent 5059065b52
commit e736e31820
2 changed files with 25 additions and 17 deletions

View File

@ -212,7 +212,7 @@ func NewClient(cfg *config.Config, consulCatalog consul.CatalogAPI, consulServic
allocUpdates: make(chan *structs.Allocation, 64),
shutdownCh: make(chan struct{}),
triggerDiscoveryCh: make(chan struct{}),
triggerNodeUpdate: make(chan struct{}),
triggerNodeUpdate: make(chan struct{}, 64),
serversDiscoveredCh: make(chan struct{}),
}
@ -961,7 +961,7 @@ func (c *Client) updateNodeFromFingerprint(response *cstructs.FingerprintRespons
c.configLock.Lock()
defer c.configLock.Unlock()
var nodeHasChanged bool
nodeHasChanged := false
for name, newVal := range response.Attributes {
oldVal := c.config.Node.Attributes[name]
@ -1020,6 +1020,9 @@ func resourcesAreEqual(first, second *structs.Resources) bool {
if first.IOPS != second.IOPS {
return false
}
if len(first.Networks) != len(second.Networks) {
return false
}
for i, e := range first.Networks {
if len(second.Networks) < i {
return false
@ -1535,22 +1538,27 @@ func (c *Client) updateNode() {
// it will update the client node copy and re-register the node.
func (c *Client) watchNodeUpdates() {
var hasChanged bool
syncTicker := time.NewTicker(c.retryIntv(nodeUpdateRetryIntv))
for {
select {
case <-time.After(c.retryIntv(nodeUpdateRetryIntv)):
if hasChanged {
c.logger.Printf("[DEBUG] client: state changed, updating node and re-registering.")
// Update the config copy.
c.configLock.Lock()
node := c.config.Node.Copy()
c.configCopy.Node = node
c.configLock.Unlock()
c.retryRegisterNode()
hasChanged = false
case <-syncTicker.C:
if !hasChanged {
continue
}
c.logger.Printf("[DEBUG] client: state changed, updating node and re-registering.")
// Update the config copy.
c.configLock.Lock()
node := c.config.Node.Copy()
c.configCopy.Node = node
c.configLock.Unlock()
c.retryRegisterNode()
hasChanged = false
syncTicker.Stop()
syncTicker = time.NewTicker(c.retryIntv(nodeUpdateRetryIntv))
case <-c.triggerNodeUpdate:
hasChanged = true
case <-c.shutdownCh:

View File

@ -104,11 +104,11 @@ func (fm *FingerprintManager) fingerprint(name string, f fingerprint.Fingerprint
return false, err
}
fm.nodeLock.Lock()
if node := fm.updateNode(&response); node != nil {
fm.nodeLock.Lock()
fm.node = node
fm.nodeLock.Unlock()
}
fm.nodeLock.Unlock()
return response.Detected, nil
}