From d1a471b2b998bff10c1b1a3b65c0f7138ade41e9 Mon Sep 17 00:00:00 2001 From: yahavi Date: Wed, 13 Sep 2023 13:44:27 +0300 Subject: [PATCH] Transfer files - Remove redundant lock in snapshot --- .../transferfiles/state/transfersnapshot.go | 20 ++++++++----------- 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/artifactory/commands/transferfiles/state/transfersnapshot.go b/artifactory/commands/transferfiles/state/transfersnapshot.go index 2cc04782f..d51b551a1 100644 --- a/artifactory/commands/transferfiles/state/transfersnapshot.go +++ b/artifactory/commands/transferfiles/state/transfersnapshot.go @@ -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 @@ -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. @@ -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) }