写文件增加负载保护

This commit is contained in:
刘祥超
2023-10-07 09:39:37 +08:00
parent c460421279
commit 79fa9d88a1
2 changed files with 60 additions and 2 deletions

View File

@@ -4,11 +4,20 @@ package fsutils
import (
"bytes"
"encoding/json"
"github.com/iwind/TeaGo/Tea"
"math"
"os"
"time"
)
const diskSpeedDataFile = "disk.speed.json"
type DiskSpeedCache struct {
Speed Speed `json:"speed"`
SpeedMB float64 `json:"speedMB"`
}
// CheckDiskWritingSpeed test disk writing speed
func CheckDiskWritingSpeed() (speedMB float64, err error) {
var tempDir = os.TempDir()
@@ -66,8 +75,13 @@ func CheckDiskIsFast() (speedMB float64, isFast bool, err error) {
if err != nil {
return
}
isFast = speedMB > 150
if speedMB <= DiskSpeedMB {
return
}
if speedMB > 1000 {
DiskSpeed = SpeedExtremelyFast
} else if speedMB > 150 {
@@ -79,8 +93,15 @@ func CheckDiskIsFast() (speedMB float64, isFast bool, err error) {
}
calculateDiskMaxWrites()
if speedMB > DiskSpeedMB {
DiskSpeedMB = speedMB
DiskSpeedMB = speedMB
// write to local file
cacheData, jsonErr := json.Marshal(&DiskSpeedCache{
Speed: DiskSpeed,
SpeedMB: DiskSpeedMB,
})
if jsonErr == nil {
_ = os.WriteFile(Tea.Root+"/data/"+diskSpeedDataFile, cacheData, 0666)
}
return