From 7985f97eb65d6f0289c32569402a70a5f6ff11b8 Mon Sep 17 00:00:00 2001 From: Laurence Date: Thu, 12 Mar 2026 12:54:02 +0000 Subject: [PATCH] perf(relay): scale packet workers and queue depth for throughput --- relay/relay.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/relay/relay.go b/relay/relay.go index 22aff76..bc1a6a6 100644 --- a/relay/relay.go +++ b/relay/relay.go @@ -9,6 +9,7 @@ import ( "io" "net" "net/http" + "runtime" "sync" "time" @@ -164,7 +165,7 @@ func NewUDPProxyServer(parentCtx context.Context, addr, serverURL string, privat addr: addr, serverURL: serverURL, privateKey: privateKey, - packetChan: make(chan Packet, 1000), + packetChan: make(chan Packet, 50000), // Increased from 1000 to handle high throughput ReachableAt: reachableAt, ctx: ctx, cancel: cancel, @@ -189,8 +190,13 @@ func (s *UDPProxyServer) Start() error { s.conn = conn logger.Info("UDP server listening on %s", s.addr) - // Start a fixed number of worker goroutines. - workerCount := 10 // TODO: Make this configurable or pick it better! + // Start worker goroutines based on CPU cores for better parallelism + // At high throughput (160+ Mbps), we need many workers to avoid bottlenecks + workerCount := runtime.NumCPU() * 10 + if workerCount < 20 { + workerCount = 20 // Minimum 20 workers + } + logger.Info("Starting %d packet workers (CPUs: %d)", workerCount, runtime.NumCPU()) for i := 0; i < workerCount; i++ { go s.packetWorker() }