From aaa2ff7fff5324d87e5e06ef36b4a305cc24e6e5 Mon Sep 17 00:00:00 2001 From: Jack Doan Date: Thu, 27 Aug 2026 12:31:08 -0500 Subject: [PATCH] respect setting for tun.pin_threads_key (#1861) --- main.go | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/main.go b/main.go index 10de68b3..ee52e7b7 100644 --- a/main.go +++ b/main.go @@ -244,10 +244,12 @@ func Main(c *config.C, configTest bool, buildVersion string, l *slog.Logger, dev if pinThreads && routines > 1 && len(cpuAffinity) == 0 && !configTest { // The operator didn't choose pin CPUs, so pick a default set that // prefers performance cores and doesn't stack co-located instances - // onto allowed[0]. The bound UDP port keys the per-instance spread: - // distinct across instances sharing a box, stable across restarts. - // A nil result keeps listenIn's stock allowed[i] fallback. - key := uint64(os.Getpid()) + // onto allowed[0]. + + // key is used to seed the spreading of routines->cores. + // use PID if you want to ensure many different Nebulas in VMs or containers land on different cores + // use port if you want to always end up on the same cores, ideal for benchmarking. + key := uint64(os.Getpid()) //default to PID pinKeyStr := strings.ToLower(c.GetString("tun.pin_threads_key", "")) switch pinKeyStr { case "": @@ -255,14 +257,16 @@ func Main(c *config.C, configTest bool, buildVersion string, l *slog.Logger, dev case "pid": l.Debug("tun.pin_threads_key is PID") case "port": - l.Info("tun.pin_threads_key is port number") + if ap, err := udpConns[0].LocalAddr(); err == nil && ap.Port() != 0 { + l.Info("tun.pin_threads_key is port number") + key = uint64(ap.Port()) + } else { + l.Warn("Failed to get a port number for tun.pin_threads_key, falling back to PID", "err", err) + } default: l.Warn("tun.pin_threads_key is invalid, using PID") } - if ap, err := udpConns[0].LocalAddr(); err == nil && ap.Port() != 0 { - key = uint64(ap.Port()) - } cpuAffinity = cpupick.Default(routines, key, l) }