[PATCH] device: use gomaxprocs for device worker count
Thomas Way
thomas at 6f.io
Fri Sep 12 15:28:51 UTC 2025
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.
Signed-off-by: Thomas Way <thomas at 6f.io>
---
device/device.go | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/device/device.go b/device/device.go
index 6854ed8..5c04913 100644
--- a/device/device.go
+++ b/device/device.go
@@ -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++ {
--
2.51.0
More information about the WireGuard
mailing list