Skip to content
Open
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
8 changes: 4 additions & 4 deletions internal/op/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"runtime"
"sort"
"strings"
"sync/atomic"
"time"

"github.com/OpenListTeam/OpenList/v4/internal/db"
Expand Down Expand Up @@ -365,7 +366,7 @@ func GetStorageVirtualFilesByPath(prefix string) []model.Obj {
return files
}

var balanceMap generic_sync.MapOf[string, int]
var balanceMap generic_sync.MapOf[string, *int64]

// GetBalancedStorage get storage by path
func GetBalancedStorage(path string) driver.Driver {
Expand All @@ -379,9 +380,8 @@ func GetBalancedStorage(path string) driver.Driver {
return storages[0]
default:
virtualPath := utils.GetActualMountPath(storages[0].GetStorage().MountPath)
i, _ := balanceMap.LoadOrStore(virtualPath, 0)
i = (i + 1) % storageNum
balanceMap.Store(virtualPath, i)
p, _ := balanceMap.LoadOrStore(virtualPath, new(int64))
i := int(atomic.AddInt64(p, 1) % int64(storageNum))
Copy link

Copilot AI Aug 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The modulo operation should be applied after converting back to int, not before. The current code performs modulo on int64 which could cause incorrect results when storageNum is large. Change to: i := int(atomic.AddInt64(p, 1)) % storageNum

Suggested change
i := int(atomic.AddInt64(p, 1) % int64(storageNum))
i := int(atomic.AddInt64(p, 1)) % storageNum

Copilot uses AI. Check for mistakes.
Comment on lines +383 to +384
Copy link

Copilot AI Aug 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The atomic counter will continuously increment and may eventually overflow. Consider using atomic.AddInt64(p, 1) - 1 and then applying modulo to get zero-based indexing, or implement a reset mechanism when the counter approaches overflow limits.

Suggested change
p, _ := balanceMap.LoadOrStore(virtualPath, new(int64))
i := int(atomic.AddInt64(p, 1) % int64(storageNum))
val := atomic.AddInt64(p, 1)
// Reset counter if approaching overflow
if val > math.MaxInt64-int64(storageNum) {
atomic.StoreInt64(p, 0)
val = 0
}
i := int(val % int64(storageNum))

Copilot uses AI. Check for mistakes.
return storages[i]
}
}