diff --git a/internal/agent/sftp/filelister.go b/internal/agent/sftp/filelister.go index 5c5a601..603e722 100644 --- a/internal/agent/sftp/filelister.go +++ b/internal/agent/sftp/filelister.go @@ -9,7 +9,6 @@ import ( "os" "path/filepath" "strings" - "sync" "github.com/pkg/sftp" "github.com/sonroyaalmerol/pbs-plus/internal/agent/cache" @@ -44,8 +43,8 @@ func (f *CustomFileInfo) Size() int64 { // Check size cache with read lock if snapSizes, ok := cache.SizeCache.Load(f.snapshotId); ok { - if cachedSize, ok := snapSizes.(*sync.Map).Load(f.filePath); ok { - return cachedSize.(int64) + if cachedSize, ok := snapSizes.(map[string]int64)[f.filePath]; ok { + return cachedSize } } @@ -61,8 +60,8 @@ func (f *CustomFileInfo) Size() int64 { return 0 } - snapSizes, _ := cache.SizeCache.LoadOrStore(f.snapshotId, &sync.Map{}) - snapSizes.(*sync.Map).Store(f.filePath, byteCount) + snapSizes, _ := cache.SizeCache.LoadOrStore(f.snapshotId, map[string]int64{}) + snapSizes.(map[string]int64)[f.filePath] = byteCount return byteCount } diff --git a/internal/agent/snapshots/windows.go b/internal/agent/snapshots/windows.go index 59c6a60..56adf92 100644 --- a/internal/agent/snapshots/windows.go +++ b/internal/agent/snapshots/windows.go @@ -121,7 +121,10 @@ func (instance *WinVSSSnapshot) UpdateTimestamp() { } func (instance *WinVSSSnapshot) Close() { - _, _ = cache.SizeCache.LoadAndDelete(instance.Id) + if fileMap, ok := cache.SizeCache.Load(instance.Id); ok { + clear(fileMap.(map[string]int64)) + cache.SizeCache.Delete(instance.Id) + } _ = vss.Remove(instance.Id) _ = os.Remove(instance.SnapshotPath) @@ -144,3 +147,19 @@ func CloseAllSnapshots() { } } } + +func GarbageCollect() { + knownSnaps := &KnownSnapshots{ + registry: "KnownSnaps", + } + + if knownSnapshots, err := knownSnaps.GetAll(); err == nil { + for _, snapshot := range knownSnapshots { + if knownSnap, err := knownSnaps.Get(snapshot.Id); err == nil { + if time.Since(knownSnap.GetTimestamp()) >= 15*time.Minute { + knownSnap.Close() + } + } + } + } +}