diff --git a/internal/rpc/services/service_server_.go b/internal/rpc/services/service_server_.go index d1cc8d1e..81f88bc6 100644 --- a/internal/rpc/services/service_server_.go +++ b/internal/rpc/services/service_server_.go @@ -6,6 +6,7 @@ import ( "github.com/TeaOSLab/EdgeAPI/internal/errors" "github.com/TeaOSLab/EdgeAPI/internal/goman" "github.com/TeaOSLab/EdgeAPI/internal/remotelogs" + "github.com/TeaOSLab/EdgeAPI/internal/utils" "github.com/iwind/TeaGo/Tea" "github.com/iwind/TeaGo/dbs" "github.com/iwind/TeaGo/types" @@ -43,6 +44,12 @@ func init() { // 导入统计数据 goman.New(func() { var duration = 30 * time.Minute + + // 小内存的要快速处理 + if utils.SystemMemoryGB() <= 2 { + duration = 15 * time.Minute + } + if Tea.IsTesting() { // 测试条件下缩短时间,以便进行观察 duration = 10 * time.Second diff --git a/internal/utils/system.go b/internal/utils/system.go new file mode 100644 index 00000000..fa040e57 --- /dev/null +++ b/internal/utils/system.go @@ -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 +} diff --git a/internal/utils/system_test.go b/internal/utils/system_test.go new file mode 100644 index 00000000..f9aa3d8d --- /dev/null +++ b/internal/utils/system_test.go @@ -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()) +}