From b7682af9dcfd49233e2190d32219e19f52a1cb7d Mon Sep 17 00:00:00 2001 From: GoEdgeLab Date: Sat, 25 Jun 2022 20:57:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=B9=E4=BA=8E=E5=B0=8F=E5=86=85=E5=AD=98?= =?UTF-8?q?=EF=BC=88=E4=B8=8D=E5=A4=A7=E4=BA=8E2G=EF=BC=89=EF=BC=8C?= =?UTF-8?q?=E7=BC=A9=E7=9F=AD=E6=9C=8D=E5=8A=A1=E7=BB=9F=E8=AE=A1=E5=AF=BC?= =?UTF-8?q?=E5=85=A5=E6=95=B0=E6=8D=AE=E5=BA=93=E7=9A=84=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/rpc/services/service_server_.go | 7 ++++++ internal/utils/system.go | 27 ++++++++++++++++++++++++ internal/utils/system_test.go | 14 ++++++++++++ 3 files changed, 48 insertions(+) create mode 100644 internal/utils/system.go create mode 100644 internal/utils/system_test.go 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()) +}