使用新的方法控制缓存并发写入速度

This commit is contained in:
GoEdgeLab
2023-07-29 09:29:36 +08:00
parent 079955b93e
commit 986ac733fd
12 changed files with 116 additions and 136 deletions

View File

@@ -65,5 +65,11 @@ func CheckDiskIsFast() (speedMB float64, isFast bool, err error) {
return
}
isFast = speedMB > 120
if speedMB > DiskSpeedMB {
DiskSpeedMB = speedMB
DiskIsFast = isFast
}
return
}

View 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)
}

View File

@@ -0,0 +1,31 @@
// Copyright 2023 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
package fsutils_test
import (
fsutils "github.com/TeaOSLab/EdgeNode/internal/utils/fs"
"github.com/iwind/TeaGo/assert"
"testing"
)
func TestWrites(t *testing.T) {
var a = assert.NewAssertion(t)
for i := 0; i < fsutils.MaxWrites+1; i++ {
fsutils.WriteBegin()
}
a.IsFalse(fsutils.WriteReady())
fsutils.WriteEnd()
a.IsTrue(fsutils.WriteReady())
}
func BenchmarkWrites(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
fsutils.WriteReady()
fsutils.WriteBegin()
fsutils.WriteEnd()
}
})
}