mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2025-11-22 22:40:30 +08:00
使用新的方法控制缓存并发写入速度
This commit is contained in:
60
internal/utils/fs/status.go
Normal file
60
internal/utils/fs/status.go
Normal file
@@ -0,0 +1,60 @@
|
||||
// Copyright 2023 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
|
||||
package fsutils
|
||||
|
||||
import (
|
||||
teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
DiskIsFast bool
|
||||
DiskSpeedMB float64
|
||||
)
|
||||
|
||||
func init() {
|
||||
if !teaconst.IsMain {
|
||||
return
|
||||
}
|
||||
|
||||
// test disk
|
||||
go func() {
|
||||
// initial check
|
||||
_, _, _ = CheckDiskIsFast()
|
||||
|
||||
// check every one hour
|
||||
var ticker = time.NewTicker(1 * time.Hour)
|
||||
var count = 0
|
||||
for range ticker.C {
|
||||
_, _, err := CheckDiskIsFast()
|
||||
if err == nil {
|
||||
count++
|
||||
if count > 24 {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
var countWrites int32 = 0
|
||||
|
||||
const MaxWrites = 32
|
||||
const MaxFastWrites = 128
|
||||
|
||||
func WriteReady() bool {
|
||||
var count = atomic.LoadInt32(&countWrites)
|
||||
if DiskIsFast {
|
||||
return count < MaxFastWrites
|
||||
}
|
||||
return count <= MaxWrites
|
||||
}
|
||||
|
||||
func WriteBegin() {
|
||||
atomic.AddInt32(&countWrites, 1)
|
||||
}
|
||||
|
||||
func WriteEnd() {
|
||||
atomic.AddInt32(&countWrites, -1)
|
||||
}
|
||||
Reference in New Issue
Block a user