From 5263805b3b557ab8628ba362a57b55a3a84ae790 Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Sat, 2 Mar 2024 00:23:52 +0800 Subject: [PATCH] chore: simplify linux nocgroup logic (#3949) --- core/stat/internal/cpu_linux.go | 10 ++++++++-- core/stat/internal/cpu_other.go | 2 +- core/stores/sqlx/stmt.go | 1 - readme-cn.md | 4 ++-- readme.md | 4 ++-- 5 files changed, 13 insertions(+), 8 deletions(-) diff --git a/core/stat/internal/cpu_linux.go b/core/stat/internal/cpu_linux.go index 29ce9dec..faf36c70 100644 --- a/core/stat/internal/cpu_linux.go +++ b/core/stat/internal/cpu_linux.go @@ -1,5 +1,3 @@ -//go:build !nocgroup - package internal import ( @@ -25,6 +23,7 @@ var ( preTotal uint64 limit float64 cores uint64 + noCgroup bool initOnce sync.Once ) @@ -32,6 +31,7 @@ var ( func initialize() { cpus, err := effectiveCpus() if err != nil { + noCgroup = true logx.Error(err) return } @@ -47,12 +47,14 @@ func initialize() { preSystem, err = systemCpuUsage() if err != nil { + noCgroup = true logx.Error(err) return } preTotal, err = cpuUsage() if err != nil { + noCgroup = true logx.Error(err) return } @@ -62,6 +64,10 @@ func initialize() { func RefreshCpu() uint64 { initOnce.Do(initialize) + if noCgroup { + return 0 + } + total, err := cpuUsage() if err != nil { return 0 diff --git a/core/stat/internal/cpu_other.go b/core/stat/internal/cpu_other.go index 0634828c..b9c27ff7 100644 --- a/core/stat/internal/cpu_other.go +++ b/core/stat/internal/cpu_other.go @@ -1,4 +1,4 @@ -//go:build !linux || nocgroup +//go:build !linux package internal diff --git a/core/stores/sqlx/stmt.go b/core/stores/sqlx/stmt.go index cc35f2b5..b66dcc7f 100644 --- a/core/stores/sqlx/stmt.go +++ b/core/stores/sqlx/stmt.go @@ -141,7 +141,6 @@ func (s statement) QueryRowsPartialCtx(ctx context.Context, v any, args ...any) func (s statement) queryRows(ctx context.Context, scanFn func(any, rowsScanner) error, v any, args ...any) error { var scanFailed bool - err := s.brk.DoWithAcceptable(func() error { return queryStmt(ctx, s.stmt, func(rows *sql.Rows) error { err := scanFn(v, rows) diff --git a/readme-cn.md b/readme-cn.md index 0263b64b..30c5c997 100644 --- a/readme-cn.md +++ b/readme-cn.md @@ -307,8 +307,8 @@ go-zero 已被许多公司用于生产部署,接入场景如在线教育、电 ## 10. CNCF 云原生技术全景图

-    - +    +

go-zero 收录在 [CNCF Cloud Native 云原生技术全景图](https://landscape.cncf.io/?selected=go-zero)。 diff --git a/readme.md b/readme.md index 63800fc7..f629149c 100644 --- a/readme.md +++ b/readme.md @@ -248,8 +248,8 @@ Join the chat via https://discord.gg/4JQvC5A4Fe ## Cloud Native Landscape

-    - +    +

go-zero enlisted in the [CNCF Cloud Native Landscape](https://landscape.cncf.io/?selected=go-zero).