agent: adding ability to pause syncing

This commit is contained in:
Armon Dadgar 2014-02-07 11:58:24 -08:00
parent d64fda8d45
commit 01b1104175
1 changed files with 26 additions and 0 deletions

View File

@ -6,6 +6,7 @@ import (
"log" "log"
"reflect" "reflect"
"sync" "sync"
"sync/atomic"
"time" "time"
) )
@ -24,6 +25,10 @@ type syncStatus struct {
// and checks. We used it to perform anti-entropy with the // and checks. We used it to perform anti-entropy with the
// catalog representation // catalog representation
type localState struct { 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 sync.Mutex
logger *log.Logger 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. // 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 // This entry is persistent and the agent will make a best effort to
// ensure it is registered // ensure it is registered
@ -200,6 +222,10 @@ SYNC:
case <-aeTimer: case <-aeTimer:
goto SYNC goto SYNC
case <-l.triggerCh: case <-l.triggerCh:
// Skip the sync if we are paused
if l.isPaused() {
continue
}
if err := l.syncChanges(); err != nil { if err := l.syncChanges(); err != nil {
l.logger.Printf("[ERR] agent: failed to sync changes: %v", err) l.logger.Printf("[ERR] agent: failed to sync changes: %v", err)
} }