在缓存写入内存之前检查磁盘是否超出容量

This commit is contained in:
GoEdgeLab
2023-10-10 14:45:14 +08:00
parent a21f07cb11
commit be43aae929

View File

@@ -422,6 +422,13 @@ func (this *FileStorage) openWriter(key string, expiredAt int64, status int, hea
return nil, ErrEntityTooLarge return nil, ErrEntityTooLarge
} }
// 检查磁盘是否超出容量
// 需要在内存缓存之前执行,避免成功写进到了内存缓存,但无法刷到磁盘
var capacityBytes = this.diskCapacityBytes()
if capacityBytes > 0 && capacityBytes <= this.TotalDiskSize()+(32<<20 /** 余量 **/) {
return nil, NewCapacityError("write file cache failed: over disk size, current: " + types.String(this.TotalDiskSize()) + ", capacity: " + types.String(capacityBytes))
}
// 先尝试内存缓存 // 先尝试内存缓存
// 我们限定仅小文件优先存在内存中 // 我们限定仅小文件优先存在内存中
var maxMemorySize = FileToMemoryMaxSize var maxMemorySize = FileToMemoryMaxSize
@@ -465,12 +472,6 @@ func (this *FileStorage) openWriter(key string, expiredAt int64, status int, hea
} }
}() }()
// 检查是否超出容量
var capacityBytes = this.diskCapacityBytes()
if capacityBytes > 0 && capacityBytes <= this.TotalDiskSize() {
return nil, NewCapacityError("write file cache failed: over disk size, current total size: " + types.String(this.TotalDiskSize()) + " bytes, capacity: " + types.String(capacityBytes))
}
var hash = stringutil.Md5(key) var hash = stringutil.Md5(key)
dir, diskIsFull := this.subDir(hash) dir, diskIsFull := this.subDir(hash)
@@ -1146,7 +1147,7 @@ func (this *FileStorage) purgeLoop() {
if requireFullLFU { if requireFullLFU {
prefix = "fully " prefix = "fully "
} }
remotelogs.Println("CACHE", prefix+"LFU purge policy '"+this.policy.Name+"' id: "+types.String(this.policy.Id)+", count: "+types.String(count)+", cost:"+fmt.Sprintf("%.2fms", time.Since(before).Seconds()*1000)) remotelogs.Println("CACHE", prefix+"LFU purge policy '"+this.policy.Name+"' id: "+types.String(this.policy.Id)+", count: "+types.String(count)+", cost: "+fmt.Sprintf("%.2fms", time.Since(before).Seconds()*1000))
if err != nil { if err != nil {
remotelogs.Warn("CACHE", "purge file storage in LFU failed: "+err.Error()) remotelogs.Warn("CACHE", "purge file storage in LFU failed: "+err.Error())