对于小内存(不大于2G),缩短服务统计导入数据库的时间

This commit is contained in:
GoEdgeLab
2022-06-25 20:57:03 +08:00
parent e6dded1965
commit b7682af9dc
3 changed files with 48 additions and 0 deletions

View File

@@ -6,6 +6,7 @@ import (
"github.com/TeaOSLab/EdgeAPI/internal/errors" "github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeAPI/internal/goman" "github.com/TeaOSLab/EdgeAPI/internal/goman"
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs" "github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/iwind/TeaGo/Tea" "github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs" "github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/types" "github.com/iwind/TeaGo/types"
@@ -43,6 +44,12 @@ func init() {
// 导入统计数据 // 导入统计数据
goman.New(func() { goman.New(func() {
var duration = 30 * time.Minute var duration = 30 * time.Minute
// 小内存的要快速处理
if utils.SystemMemoryGB() <= 2 {
duration = 15 * time.Minute
}
if Tea.IsTesting() { if Tea.IsTesting() {
// 测试条件下缩短时间,以便进行观察 // 测试条件下缩短时间,以便进行观察
duration = 10 * time.Second duration = 10 * time.Second

27
internal/utils/system.go Normal file
View File

@@ -0,0 +1,27 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package utils
import (
"github.com/shirou/gopsutil/v3/mem"
)
var systemTotalMemory = -1
func init() {
_ = SystemMemoryGB()
}
func SystemMemoryGB() int {
if systemTotalMemory > 0 {
return systemTotalMemory
}
stat, err := mem.VirtualMemory()
if err != nil {
return 0
}
systemTotalMemory = int(stat.Total / 1024 / 1024 / 1024)
return systemTotalMemory
}

View File

@@ -0,0 +1,14 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package utils_test
import (
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"testing"
)
func TestSystemMemoryGB(t *testing.T) {
t.Log(utils.SystemMemoryGB())
t.Log(utils.SystemMemoryGB())
t.Log(utils.SystemMemoryGB())
}