Skip to content

Commit

Permalink
Transfer files - Remove redundant lock in snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
yahavi committed Sep 13, 2023
1 parent 07ca5e0 commit d1a471b
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions artifactory/commands/transferfiles/state/transfersnapshot.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package state

import (
"github.com/jfrog/jfrog-cli-core/v2/utils/reposnapshot"
"github.com/jfrog/jfrog-client-go/utils/errorutils"
"sync"
"time"

"github.com/jfrog/jfrog-cli-core/v2/utils/reposnapshot"
"github.com/jfrog/jfrog-client-go/utils/errorutils"
)

var saveRepoSnapshotMutex sync.Mutex

type SnapshotActionFunc func(rts *RepoTransferSnapshot) error

var snapshotSaveIntervalMin = snapshotSaveIntervalMinDefault
Expand All @@ -20,8 +23,7 @@ type RepoTransferSnapshot struct {
snapshotManager reposnapshot.RepoSnapshotManager
lastSaveTimestamp time.Time
// This boolean marks that this snapshot continues a previous run. It allows skipping certain checks if it was not loaded, because some data is known to be new.
loadedFromSnapshot bool
saveRepoSnapshotMutex sync.Mutex
loadedFromSnapshot bool
}

// Runs the provided action on the snapshot manager, and periodically saves the repo state and snapshot to the snapshot dir.
Expand All @@ -38,22 +40,16 @@ func (ts *TransferStateManager) snapshotAction(action SnapshotActionFunc) (err e
return nil
}

if !ts.repoTransferSnapshot.saveRepoSnapshotMutex.TryLock() {
if !saveRepoSnapshotMutex.TryLock() {
return nil
}
defer ts.repoTransferSnapshot.saveRepoSnapshotMutex.Unlock()
defer saveRepoSnapshotMutex.Unlock()

ts.repoTransferSnapshot.lastSaveTimestamp = now
if err = ts.repoTransferSnapshot.snapshotManager.PersistRepoSnapshot(); err != nil {
return err
}

return saveStateToSnapshot(&ts.TransferState)
}

func saveStateToSnapshot(ts *TransferState) error {
saveStateMutex.Lock()
defer saveStateMutex.Unlock()
return ts.persistTransferState(true)
}

Expand Down

0 comments on commit d1a471b

Please sign in to comment.