Skip to contentSkip to conversation

device: use GOMAXPROCS for worker count

PATCH New [email protected] 1 participant 1 message Oct 5

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 device: use GOMAXPROCS for worker count

From: kurochan <[email protected]>

Use runtime.GOMAXPROCS(0) instead of runtime.NumCPU() when starting encryption, decryption, and handshake workers. This makes the worker pools follow the runtime's effective parallelism.

Go 1.25 introduced automatic GOMAXPROCS adjustment based on cgroup CPU limits and other runtime constraints. However, this module declares go 1.23.1, so the compatibility defaults are:

containermaxprocs=0
updatemaxprocs=0

As a result, cgroup-based adjustment is not enabled by default for this module when built with a newer Go toolchain. Using GOMAXPROCS still allows the configured parallelism to be reflected in the worker count, and keeps the worker lifecycle ready for future cgroup-aware runtime defaults.

---
 device/device.go | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
device/device.go +6 -3
index 6854ed8..7d9d910 100644
@@ -308,10 +308,13 @@ func NewDevice(tunDevice tun.Device, bind conn.Bind, logger *Logger) *Device {
 
 	// start workers
 
-	cpus := runtime.NumCPU()
+	workers := runtime.GOMAXPROCS(0)
+	if workers < 2 {
+		workers = 2
+	}
 	device.state.stopping.Wait()
-	device.queue.encryption.wg.Add(cpus) // One for each RoutineHandshake
-	for i := 0; i < cpus; i++ {
+	device.queue.encryption.wg.Add(workers) // One for each RoutineHandshake
+	for i := 0; i < workers; i++ {
 		go device.RoutineEncryption(i + 1)
 		go device.RoutineDecryption(i + 1)
 		go device.RoutineHandshake(i + 1)

base-commit: 8eb8581c3460a654ad0926891e7e5f4df75f01a2

Signature · 1 line
2.55.0