fix: update existing limiter rates & adapt flush test for dedup

This commit is contained in:
xboard
2026-03-25 18:31:37 +08:00
parent b51cecfe17
commit 691ca93c82
2 changed files with 19 additions and 5 deletions
+16 -2
View File
@@ -43,8 +43,7 @@ func (t *SpeedTracker) SetLogCallback(f SpeedTrackerLogCallback) {
t.logFunc = f
}
// UpdateBuckets updates the UUID→userID mapping and cleans up removed users.
// Rate limiters are created on-demand in GetLimiter.
// UpdateBuckets updates the UUID→userID mapping and syncs existing limiters.
func (t *SpeedTracker) UpdateBuckets() {
currentUsers := make([]panel.User, 0, 32)
t.limiter.mu.RLock()
@@ -65,6 +64,21 @@ func (t *SpeedTracker) UpdateBuckets() {
if user.UUID != "" {
newUUIDMap[user.UUID] = user.ID
}
// Update existing limiter if speed changed
if lim, ok := t.buckets[user.ID]; ok {
if user.SpeedLimit > 0 {
bytesPerSec := int(user.SpeedLimit) * 1_000_000 / 8
burst := bytesPerSec
if burst < 64*1024 {
burst = 64 * 1024
}
lim.SetLimit(rate.Limit(bytesPerSec))
lim.SetBurst(burst)
} else {
delete(t.buckets, user.ID)
}
}
}
// Clean up buckets for removed users