From 01b11041750af9d9f50c8b069334c95bb9f9537f Mon Sep 17 00:00:00 2001 From: Armon Dadgar Date: Fri, 7 Feb 2014 11:58:24 -0800 Subject: [PATCH] agent: adding ability to pause syncing --- command/agent/local.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/command/agent/local.go b/command/agent/local.go index f4d3b9a96f..f7a0d376df 100644 --- a/command/agent/local.go +++ b/command/agent/local.go @@ -6,6 +6,7 @@ import ( "log" "reflect" "sync" + "sync/atomic" "time" ) @@ -24,6 +25,10 @@ type syncStatus struct { // and checks. We used it to perform anti-entropy with the // catalog representation type localState struct { + // paused is used to check if we are paused. Must be the first + // element due to a go bug. + paused int32 + sync.Mutex logger *log.Logger @@ -66,6 +71,23 @@ func (l *localState) changeMade() { } } +// Pause is used to pause state syncronization, this can be +// used to make batch changes +func (l *localState) Pause() { + atomic.StoreInt32(&l.paused, 1) +} + +// Unpause is used to resume state syncronization +func (l *localState) Unpause() { + atomic.StoreInt32(&l.paused, 0) + l.changeMade() +} + +// isPaused is used to check if we are paused +func (l *localState) isPaused() bool { + return atomic.LoadInt32(&l.paused) == 1 +} + // AddService is used to add a service entry to the local state. // This entry is persistent and the agent will make a best effort to // ensure it is registered @@ -200,6 +222,10 @@ SYNC: case <-aeTimer: goto SYNC case <-l.triggerCh: + // Skip the sync if we are paused + if l.isPaused() { + continue + } if err := l.syncChanges(); err != nil { l.logger.Printf("[ERR] agent: failed to sync changes: %v", err) }