fix(storage): release replaced bytes when a key is overwritten

Put added the new size to the usage counter without subtracting the file it
replaced. Each overwrite inflated usage until restart and could trip the
quota on a cache that was not full.
This commit is contained in:
2026-11-23 13:15:33 +00:00
parent 4713524778
commit 1f31256769
2 changed files with 45 additions and 2 deletions
+7 -2
View File
@@ -162,7 +162,12 @@ func (fs *FilesystemStorage) Put(ctx context.Context, key string, data io.Reader
// only successfully renamed files. Quota check happens before increment
// to avoid transient inflation seen by concurrent Puts.
fs.mu.Lock()
if fs.quota > 0 && fs.used+written > fs.quota {
// An overwrite releases the bytes of the file it replaces.
var replaced int64
if info, statErr := root.Stat(relPath); statErr == nil {
replaced = info.Size()
}
if fs.quota > 0 && fs.used-replaced+written > fs.quota {
fs.mu.Unlock()
_ = root.Remove(tempPath)
metrics.RecordStorageOperation("filesystem", "put", "quota_exceeded")
@@ -174,7 +179,7 @@ func (fs *FilesystemStorage) Put(ctx context.Context, key string, data io.Reader
metrics.RecordStorageOperation("filesystem", "put", "error")
return errors.Wrap(err, errors.ErrCodeStorageFailure, "failed to rename temp file")
}
fs.used += written
fs.used += written - replaced
currentUsed := fs.used
fs.mu.Unlock()