From 0bc093a1aae4fb5e101fae815fe6673e9180923e Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Fri, 3 May 2024 13:14:00 +0200 Subject: [PATCH] internal/sysinfo: use sync.OnceValue for CPUName Change-Id: I0f3ae97f2bd5ff3f533c5bf4570a8cda8b92b16a Reviewed-on: https://go-review.googlesource.com/c/go/+/582836 Reviewed-by: David Chase Reviewed-by: Ian Lance Taylor LUCI-TryBot-Result: Go LUCI Auto-Submit: Tobias Klauser --- src/internal/sysinfo/sysinfo.go | 29 +++++++++-------------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/src/internal/sysinfo/sysinfo.go b/src/internal/sysinfo/sysinfo.go index 6a29ad2bc1..ae0d5a440c 100644 --- a/src/internal/sysinfo/sysinfo.go +++ b/src/internal/sysinfo/sysinfo.go @@ -11,25 +11,14 @@ import ( "sync" ) -var cpuInfo struct { - once sync.Once - name string -} +var CPUName = sync.OnceValue(func() string { + if name := cpu.Name(); name != "" { + return name + } -func CPUName() string { - cpuInfo.once.Do(func() { - // Try to get the information from internal/cpu. - if name := cpu.Name(); name != "" { - cpuInfo.name = name - return - } + if name := osCpuInfoName(); name != "" { + return name + } - // TODO(martisch): use /proc/cpuinfo and /sys/devices/system/cpu/ on Linux as fallback. - if name := osCpuInfoName(); name != "" { - cpuInfo.name = name - return - } - }) - - return cpuInfo.name -} + return "" +})