Skip to content

Commit

Permalink
vault: CheckSync tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel committed Sep 23, 2020
1 parent 7f2c373 commit 5322c03
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
36 changes: 31 additions & 5 deletions vault/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ func (v *Vault) SyncStatus() (*SyncStatus, error) {
// - Clear status (last synced, push, pull, nonces, rsalt)
// - Clear remote
func (v *Vault) Unsync(ctx context.Context) error {
v.mtx.Lock()
defer v.mtx.Unlock()
logger.Infof("Unsyncing...")

if v.remote == nil {
return errors.Errorf("no remote set")
}
Expand Down Expand Up @@ -192,8 +196,10 @@ func (v *Vault) SyncEnabled() (bool, error) {
return true, nil
}

// CheckSync performs sync unless disabled or already synced recently (within expire duration).
func (v *Vault) CheckSync(ctx context.Context, expire time.Duration) (bool, error) {
func (v *Vault) shouldCheck(expire time.Duration) (bool, error) {
v.checkMtx.Lock()
defer v.checkMtx.Unlock()

enabled, err := v.SyncEnabled()
if err != nil {
return false, err
Expand All @@ -202,17 +208,37 @@ func (v *Vault) CheckSync(ctx context.Context, expire time.Duration) (bool, erro
return false, nil
}

diffCheck := v.clock.Now().Sub(v.checkedAt)
if diffCheck >= 0 && diffCheck < expire {
logger.Debugf("Already checked recently")
return false, nil
}
v.checkedAt = v.clock.Now()

last, err := v.lastSync()
if err != nil {
return false, err
}
diff := v.clock.Now().Sub(last)
if diff >= 0 && diff < expire {
logger.Debugf("Last synced: %s", last)
diffLast := v.clock.Now().Sub(last)
if diffLast >= 0 && diffLast < expire {
logger.Debugf("Already synced recently")
return false, nil
}

logger.Debugf("Last synced: %s", last)
return true, nil
}

// CheckSync performs sync unless disabled or already synced recently (within expire duration).
func (v *Vault) CheckSync(ctx context.Context, expire time.Duration) (bool, error) {
enabled, err := v.shouldCheck(expire)
if err != nil {
return false, err
}
if !enabled {
return false, nil
}

if err := v.Sync(ctx); err != nil {
return true, err
}
Expand Down
19 changes: 19 additions & 0 deletions vault/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ func testSync(t *testing.T, st1 vault.Store, st2 vault.Store) {
require.Equal(t, "key1", out.ID)
require.Equal(t, []byte("mysecretdata.1a"), out.Data)

// CheckSync not enabled
synced, err := v1.CheckSync(ctx, time.Duration(0))
require.NoError(t, err)
require.False(t, synced)

err = v1.Sync(ctx)
require.NoError(t, err)

Expand Down Expand Up @@ -164,6 +169,20 @@ func testSync(t *testing.T, st1 vault.Store, st2 vault.Store) {
require.NoError(t, err)
require.Nil(t, out)

// CheckSync
synced, err = v1.CheckSync(ctx, time.Duration(0))
require.NoError(t, err)
require.True(t, synced)

synced, err = v1.CheckSync(ctx, time.Duration(time.Second))
require.NoError(t, err)
require.False(t, synced)

time.Sleep(time.Millisecond)
synced, err = v1.CheckSync(ctx, time.Duration(time.Millisecond))
require.NoError(t, err)
require.True(t, synced)

history, err = v1.ItemHistory("key1")
require.NoError(t, err)
require.Equal(t, 5, len(history))
Expand Down
3 changes: 3 additions & 0 deletions vault/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ type Vault struct {

auto *time.Timer

checkedAt time.Time
checkMtx sync.Mutex

subs *subscribers
}

Expand Down

0 comments on commit 5322c03

Please sign in to comment.