Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transfer files - Remove redundant lock in snapshot #956

Merged
merged 2 commits into from
Sep 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading