net/udprelay: use GOMAXPROCS instead of NumCPU for socket count

runtime.NumCPU() returns the number of CPUs on the host, which in
containerized environments is the node's CPU count rather than the
container's CPU limit. This causes excessive memory allocation in
pods with low CPU requests running on large nodes, as each socket's
packetReadLoop allocates significant buffer memory.

Use runtime.GOMAXPROCS(0) instead, which is container-aware since
Go 1.25 and respects CPU limits set via cgroups.

Fixes #18774

Signed-off-by: Daniel Pañeda <daniel.paneda@clickhouse.com>
This commit is contained in:
Daniel Pañeda
2026-03-04 17:51:01 +01:00
committed by Jordan Whited
parent 26951a1cbb
commit d58bfb8a1b

View File

@@ -651,8 +651,9 @@ func trySetSOMark(logf logger.Logf, netMon *netmon.Monitor, network, address str
// single packet syscall operations.
func (s *Server) bindSockets(desiredPort uint16) error {
// maxSocketsPerAF is a conservative starting point, but is somewhat
// arbitrary.
maxSocketsPerAF := min(16, runtime.NumCPU())
// arbitrary. Use GOMAXPROCS rather than NumCPU as it is container-aware
// and respects CPU limits/quotas set via cgroups.
maxSocketsPerAF := min(16, runtime.GOMAXPROCS(0))
listenConfig := &net.ListenConfig{
Control: func(network, address string, c syscall.RawConn) error {
trySetReusePort(network, address, c)