Skip to contentSkip to conversation

device: use gomaxprocs for device worker count

PATCH New Thomas Way 1 participant 1 message Feb 8

lore
Share & tools
lore

Thomas Way

author
#0 lore raw

PATCH 1/1 device: use gomaxprocs for device worker count

wireguard-go currently creates a worker for each logical CPU available on the host, which can be problematic in containerised environments where the actual number of logical CPUs available may be significantly less. Many modern server CPUs have hundreds of logical CPUs, and containers often have a limit of at most 1-4. Such a large disparity can cause significant performance degradation, and increase both CPU throttling and latency.

The Go runtime has recently been updated to be aware of container CPU limits to solve this.

https://go.dev/blog/container-aware-gomaxprocs

Using GOMAXPROCS(0) instead of NumCPU means that fewer workers will be started in environments which can't make use of them, and should improve the performance of wireguard-go. Regardless, it seems like the sensible thing to do in-case the main program manually sets GOMAXPROCS to a value other than NumCPU.

---
 device/device.go | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
device/device.go +1 -1
index 6854ed8..5c04913 100644
@@ -308,7 +308,7 @@ func NewDevice(tunDevice tun.Device, bind conn.Bind, logger *Logger) *Device {
 
 	// start workers
 
-	cpus := runtime.NumCPU()
+	cpus := runtime.GOMAXPROCS(0)
 	device.state.stopping.Wait()
 	device.queue.encryption.wg.Add(cpus) // One for each RoutineHandshake
 	for i := 0; i < cpus; i++ {
Signature · 1 line
2.51.0