Explorar el Código

pkg/sysinfo: unify NumCPU implementation

Use a single exported implementation, so that we can maintain the
GoDoc string in one place, and use non-exported functions for the
actual implementation (which were already in place).

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Sebastiaan van Stijn hace 2 años
padre
commit
6919b9879b

+ 6 - 4
pkg/sysinfo/numcpu.go

@@ -1,13 +1,15 @@
-//go:build !linux && !windows
-// +build !linux,!windows
-
 package sysinfo // import "github.com/docker/docker/pkg/sysinfo"
 
 import (
 	"runtime"
 )
 
-// NumCPU returns the number of CPUs
+// NumCPU returns the number of CPUs. On Linux and Windows, it returns
+// the number of CPUs which are currently online. On other platforms,
+// it's the equivalent of [runtime.NumCPU].
 func NumCPU() int {
+	if ncpu := numCPU(); ncpu > 0 {
+		return ncpu
+	}
 	return runtime.NumCPU()
 }

+ 0 - 10
pkg/sysinfo/numcpu_linux.go

@@ -1,8 +1,6 @@
 package sysinfo // import "github.com/docker/docker/pkg/sysinfo"
 
 import (
-	"runtime"
-
 	"golang.org/x/sys/unix"
 )
 
@@ -23,11 +21,3 @@ func numCPU() int {
 
 	return mask.Count()
 }
-
-// NumCPU returns the number of CPUs which are currently online
-func NumCPU() int {
-	if ncpu := numCPU(); ncpu > 0 {
-		return ncpu
-	}
-	return runtime.NumCPU()
-}

+ 9 - 0
pkg/sysinfo/numcpu_other.go

@@ -0,0 +1,9 @@
+//go:build !linux && !windows
+// +build !linux,!windows
+
+package sysinfo
+
+func numCPU() int {
+	// not implemented
+	return 0
+}

+ 0 - 9
pkg/sysinfo/numcpu_windows.go

@@ -1,7 +1,6 @@
 package sysinfo // import "github.com/docker/docker/pkg/sysinfo"
 
 import (
-	"runtime"
 	"unsafe"
 
 	"golang.org/x/sys/windows"
@@ -35,11 +34,3 @@ func numCPU() int {
 	ncpu := int(popcnt(uint64(mask)))
 	return ncpu
 }
-
-// NumCPU returns the number of CPUs which are currently online
-func NumCPU() int {
-	if ncpu := numCPU(); ncpu > 0 {
-		return ncpu
-	}
-	return runtime.NumCPU()
-}