Skip to content

Commit

Permalink
universe: prevent syncer from running concurrently
Browse files Browse the repository at this point in the history
  • Loading branch information
guggero committed Aug 18, 2023
1 parent 9a17cde commit e308cdc
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions universe/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"sync"

"github.com/davecgh/go-spew/spew"
"github.com/lightninglabs/taproot-assets/fn"
Expand Down Expand Up @@ -37,6 +38,14 @@ type SimpleSyncCfg struct {
// on a set difference operation between the local and remote Universe.
type SimpleSyncer struct {
cfg SimpleSyncCfg

// isSyncing keeps track of whether we're currently syncing the local
// Universe with a remote Universe. This is used to prevent concurrent
// syncs.
isSyncing bool

// isSyncingMtx is a mutex that protects the isSyncing flag.
isSyncingMtx sync.Mutex
}

// NewSimpleSyncer creates a new SimpleSyncer instance.
Expand All @@ -52,6 +61,23 @@ func NewSimpleSyncer(cfg SimpleSyncCfg) *SimpleSyncer {
func (s *SimpleSyncer) executeSync(ctx context.Context, diffEngine DiffEngine,
syncType SyncType, idsToSync []Identifier) ([]AssetSyncDiff, error) {

// Prevent the syncer from running twice. We also don't want to block on
// acquiring the mutex, so we use a secondary flag that is guarded by a
// mutex instead.
s.isSyncingMtx.Lock()
if s.isSyncing {
return nil, fmt.Errorf("sync is already in progress, please " +
"wait for it to finish")
}
s.isSyncing = true
s.isSyncingMtx.Unlock()

defer func() {
s.isSyncingMtx.Lock()
s.isSyncing = false
s.isSyncingMtx.Unlock()
}()

var (
targetRoots []BaseRoot
err error
Expand Down

0 comments on commit e308cdc

Please sign in to comment.