agent: refactor sync loop to linear flow of control

This commit is contained in:
Frank Schroeder 2017-08-28 14:17:10 +02:00
parent 8cc4ee886d
commit 218cd4d501
No known key found for this signature in database
GPG Key ID: 4D65C6EAEC87DECD
1 changed files with 49 additions and 48 deletions

View File

@ -93,66 +93,67 @@ func (s *StateSyncer) Run() {
Sync: Sync:
for { for {
// update the sync status switch err := s.State.UpdateSyncState(); {
err := s.State.UpdateSyncState()
if err == nil {
break
}
s.Logger.Printf("[ERR] agent: failed to sync remote state: %v", err) // update sync status failed
case err != nil:
s.Logger.Printf("[ERR] agent: failed to sync remote state: %v", err)
// retry updating sync status after some time or when a consul // retry updating sync status after some time or when a consul
// server was added. // server was added.
select {
// consul server added to cluster.
// retry sooner than retryFailIntv to converge cluster quicker
// but stagger delay to avoid thundering herd
case <-s.ServerUpCh:
select { select {
case <-time.After(stagger(serverUpIntv)):
// consul server added to cluster.
// retry sooner than retryFailIntv to converge cluster sooner
// but stagger delay to avoid thundering herd
case <-s.ServerUpCh:
select {
case <-time.After(stagger(serverUpIntv)):
case <-s.ShutdownCh:
return
}
// retry full sync after some time
// todo(fs): why don't we use s.Interval here?
case <-time.After(retryFailIntv + stagger(retryFailIntv)):
case <-s.ShutdownCh: case <-s.ShutdownCh:
return return
} }
// retry full sync after some time // update sync status OK
// todo(fs): why don't we use s.Interval here? default:
case <-time.After(retryFailIntv + stagger(retryFailIntv)): // force-trigger sync to pickup any changes
s.triggerSync()
case <-s.ShutdownCh: // do partial syncs until it is time for a full sync again
return for {
} select {
} // todo(fs): why don't we honor the ServerUpCh here as well?
// todo(fs): by default, s.Interval is 60s which is >> 3s (serverUpIntv)
// case <-s.ServerUpCh:
// select {
// case <-time.After(stagger(serverUpIntv)):
// continue Sync
// case <-s.ShutdownCh:
// return
// }
// Force-trigger sync to pickup any changes case <-time.After(s.Interval + stagger(s.Interval)):
s.triggerSync() continue Sync
// Wait for sync events case <-s.TriggerCh:
for { if s.Paused() {
select { continue
// todo(fs): why don't we honor the ServerUpCh here as well? }
// todo(fs): by default, s.Interval is 60s which is >> 3s (serverUpIntv) if err := s.State.SyncChanges(); err != nil {
// case <-s.ServerUpCh: s.Logger.Printf("[ERR] agent: failed to sync changes: %v", err)
// select { }
// case <-time.After(stagger(serverUpIntv)):
// continue Sync
// case <-s.ShutdownCh:
// return
// }
case <-time.After(s.Interval + stagger(s.Interval)): case <-s.ShutdownCh:
goto Sync return
}
case <-s.TriggerCh:
if s.Paused() {
continue
} }
if err := s.State.SyncChanges(); err != nil {
s.Logger.Printf("[ERR] agent: failed to sync changes: %v", err)
}
case <-s.ShutdownCh:
return
} }
} }
} }