mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2026-01-06 07:45:47 +08:00
写入和删除缓存文件时增加线程数限制
This commit is contained in:
@@ -91,7 +91,6 @@ func CheckDiskIsFast() (speedMB float64, isFast bool, err error) {
|
||||
} else {
|
||||
DiskSpeed = SpeedExtremelySlow
|
||||
}
|
||||
calculateDiskMaxWrites()
|
||||
|
||||
DiskSpeedMB = speedMB
|
||||
|
||||
|
||||
31
internal/utils/fs/os.go
Normal file
31
internal/utils/fs/os.go
Normal file
@@ -0,0 +1,31 @@
|
||||
// Copyright 2024 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
|
||||
package fsutils
|
||||
|
||||
import "os"
|
||||
|
||||
func Remove(filename string) (err error) {
|
||||
WriterLimiter.Ack()
|
||||
err = os.Remove(filename)
|
||||
WriterLimiter.Release()
|
||||
return
|
||||
}
|
||||
|
||||
func Rename(oldPath string, newPath string) (err error) {
|
||||
WriterLimiter.Ack()
|
||||
err = os.Rename(oldPath, newPath)
|
||||
WriterLimiter.Release()
|
||||
return
|
||||
}
|
||||
|
||||
func ReadFile(filename string) (data []byte, err error) {
|
||||
ReaderLimiter.Ack()
|
||||
data, err = os.ReadFile(filename)
|
||||
ReaderLimiter.Release()
|
||||
return
|
||||
}
|
||||
|
||||
func WriteFile(filename string, data []byte, perm os.FileMode) (err error) {
|
||||
err = os.WriteFile(filename, data, perm)
|
||||
return
|
||||
}
|
||||
@@ -9,7 +9,6 @@ import (
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/shirou/gopsutil/v3/load"
|
||||
"os"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -37,9 +36,8 @@ const (
|
||||
)
|
||||
|
||||
var (
|
||||
DiskSpeed = SpeedLow
|
||||
DiskMaxWrites int32 = 32
|
||||
DiskSpeedMB float64
|
||||
DiskSpeed = SpeedLow
|
||||
DiskSpeedMB float64
|
||||
)
|
||||
|
||||
var IsInHighLoad = false
|
||||
@@ -65,7 +63,6 @@ func init() {
|
||||
if err == nil && cache.SpeedMB > 0 {
|
||||
DiskSpeedMB = cache.SpeedMB
|
||||
DiskSpeed = cache.Speed
|
||||
calculateDiskMaxWrites()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -109,39 +106,6 @@ func DiskIsExtremelyFast() bool {
|
||||
return DiskSpeed == SpeedExtremelyFast
|
||||
}
|
||||
|
||||
var countWrites int32 = 0
|
||||
|
||||
func WriteReady() bool {
|
||||
if IsInExtremelyHighLoad {
|
||||
return false
|
||||
}
|
||||
|
||||
return atomic.LoadInt32(&countWrites) < DiskMaxWrites
|
||||
}
|
||||
|
||||
func WriteBegin() {
|
||||
atomic.AddInt32(&countWrites, 1)
|
||||
}
|
||||
|
||||
func WriteEnd() {
|
||||
atomic.AddInt32(&countWrites, -1)
|
||||
}
|
||||
|
||||
func calculateDiskMaxWrites() {
|
||||
switch DiskSpeed {
|
||||
case SpeedExtremelyFast:
|
||||
DiskMaxWrites = 32
|
||||
case SpeedFast:
|
||||
DiskMaxWrites = 16
|
||||
case SpeedLow:
|
||||
DiskMaxWrites = 8
|
||||
case SpeedExtremelySlow:
|
||||
DiskMaxWrites = 4
|
||||
default:
|
||||
DiskMaxWrites = 4
|
||||
}
|
||||
}
|
||||
|
||||
// WaitLoad wait system load to downgrade
|
||||
func WaitLoad(maxLoad float64, maxLoops int, delay time.Duration) {
|
||||
for i := 0; i < maxLoops; i++ {
|
||||
|
||||
@@ -4,33 +4,10 @@ package fsutils_test
|
||||
|
||||
import (
|
||||
fsutils "github.com/TeaOSLab/EdgeNode/internal/utils/fs"
|
||||
"github.com/iwind/TeaGo/assert"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestWrites(t *testing.T) {
|
||||
var a = assert.NewAssertion(t)
|
||||
|
||||
for i := 0; i < int(fsutils.DiskMaxWrites); i++ {
|
||||
fsutils.WriteBegin()
|
||||
}
|
||||
a.IsFalse(fsutils.WriteReady())
|
||||
|
||||
fsutils.WriteEnd()
|
||||
a.IsTrue(fsutils.WriteReady())
|
||||
}
|
||||
|
||||
func TestWaitLoad(t *testing.T) {
|
||||
fsutils.WaitLoad(100, 5, 1*time.Minute)
|
||||
}
|
||||
|
||||
func BenchmarkWrites(b *testing.B) {
|
||||
b.RunParallel(func(pb *testing.PB) {
|
||||
for pb.Next() {
|
||||
fsutils.WriteReady()
|
||||
fsutils.WriteBegin()
|
||||
fsutils.WriteEnd()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user