Skip to content

Commit

Permalink
add mutex for snapshot global
Browse files Browse the repository at this point in the history
  • Loading branch information
Son Roy Almerol committed Nov 4, 2024
1 parent da0c208 commit 1acce9c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
2 changes: 1 addition & 1 deletion internal/agent/sftp/sftp.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func handleSFTP(channel ssh.Channel, driveLetter string) {
ctx := context.Background()
sftpHandler, err := NewSftpHandler(ctx, driveLetter, snapshot)
if err != nil {
_ = snapshot.Close()
snapshot.Close()
utils.ShowMessageBox("Fatal Error", fmt.Sprintf("failed to initialize handler: %s", err))
os.Exit(1)
}
Expand Down
18 changes: 14 additions & 4 deletions internal/agent/snapshots/windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"path/filepath"
"strings"
"sync"
"time"

"github.com/mxk/go-vss"
Expand All @@ -20,6 +21,7 @@ type WinVSSSnapshot struct {
}

var KnownSnapshots []*WinVSSSnapshot
var SnapLock sync.Mutex

func getAppDataFolder() (string, error) {
configDir, err := os.UserConfigDir()
Expand Down Expand Up @@ -52,11 +54,13 @@ func Snapshot(driveLetter string) (*WinVSSSnapshot, error) {
if _, err := vss.Get(snapshotPath); err == nil {
if KnownSnapshots != nil {
for _, knownSnap := range KnownSnapshots {
if knownSnap.SnapshotPath == snapshotPath && time.Since(knownSnap.LastAccessed) < time.Hour {
if knownSnap.SnapshotPath == snapshotPath && time.Since(knownSnap.LastAccessed) < (15*time.Minute) {
return knownSnap, nil
} else if time.Since(knownSnap.LastAccessed) >= time.Hour {
} else if time.Since(knownSnap.LastAccessed) >= (15 * time.Minute) {
knownSnap.Close()
break
if knownSnap.SnapshotPath == snapshotPath {
break
}
}
}
}
Expand Down Expand Up @@ -85,15 +89,21 @@ func Snapshot(driveLetter string) (*WinVSSSnapshot, error) {
Id: sc.ID,
}

SnapLock.Lock()
if KnownSnapshots == nil {
KnownSnapshots = make([]*WinVSSSnapshot, 0)
KnownSnapshots = append(KnownSnapshots, &newSnapshot)
}

KnownSnapshots = append(KnownSnapshots, &newSnapshot)
SnapLock.Unlock()

return &newSnapshot, nil
}

func (instance *WinVSSSnapshot) Close() {
SnapLock.Lock()
defer SnapLock.Unlock()

_ = vss.Remove(instance.Id)

if KnownSnapshots != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/store/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (storeInstance *Store) GetMostRecentTask(job *Job, since *time.Time) (*Task
var resp TasksResponse
err := storeInstance.ProxmoxHTTPRequest(
http.MethodGet,
fmt.Sprintf("/api2/json/nodes/localhost/tasks?store=%s&typefilter=backup&limit=1&since=%d", job.Store, since.Unix()),
fmt.Sprintf("/api2/json/nodes/localhost/tasks?store=%s&typefilter=backup&start=0&limit=1&since=%d", job.Store, since.Unix()),
nil,
&resp,
)
Expand Down

0 comments on commit 1acce9c

Please sign in to comment.