next.js
04aeaa02 - switchyard: run Go code on at most 4 threads (#101901)

Commit
9 days ago
switchyard: run Go code on at most 4 threads (#101901) ### What? Switchyard (the Go server in front of `next start`, added in #101695) now runs Go code on at most 4 threads. Machines with 4 or fewer cores keep Go's default, and a `GOMAXPROCS` environment variable still wins (the launcher passes its environment through). ### Why? Go's default `GOMAXPROCS` is one thread per core the process may use. Switchyard's work per request is small, so on machines with many cores most of those threads spend their CPU waking each other and in Go's scheduler, which costs more than the forwarding itself. Capping the threads cuts Switchyard's CPU per request. **Why 4:** - **Not 2** (this PR's first version): for route.go requests, which Switchyard sends straight to the Go worker without Node (#101981), 2 threads became the ceiling. At 256 concurrent requests Switchyard used 1.94 of its 2 cores and topped out at 46k req/s on 8 CPUs, against 74k with 4 threads; on 4 CPUs it was 21% below no limit. - **Not half the cores:** that gives 2 on a 4-core machine, which is exactly that losing case. - **Not no limit:** on 8 CPUs, no limit spends 72–88 µs of Switchyard CPU per direct request against 48–61 µs with 4 threads, and is slower at high concurrency (61k vs 74k req/s). - **4** was the best or tied-best setting in every direct-path run. It matches Go's default on 4 cores, and through Node it keeps most of the CPU saving. ### How? At startup, `main.go` sets `runtime.GOMAXPROCS(4)` unless the environment sets `GOMAXPROCS` to a positive number (Go ignores other values, so the cap still applies then) or Go's default is already 4 or lower. It's done in `main.go` rather than in the launcher, so it applies however Switchyard is started, and the rule about which values Go accepts stays in the Go code. ### Measurements Linux `node:22` container under Docker Desktop on Apple silicon (arm64, not x86). Node, Switchyard and the Go worker are pinned to 4 or 8 CPUs, and the load generator runs on other CPUs. Keep-alive, 2 s warm-up and 8 s measured, median (min–max) of 3 rounds. "Switchyard µs" is Switchyard's CPU time per request. The host was busy, so compare rows within a table, not across tables. **Direct route.go requests** (trivial `route.go` returning `ok`, #101981's head `1589b5bd6e` with each setting built locally): | Setting | 8 CPUs, 64 concurrent | 8 CPUs, 256 concurrent | 4 CPUs, 64 concurrent | 4 CPUs, 256 concurrent | | --- | --- | --- | --- | --- | | no limit | 42.9k req/s, p99 4.2 ms, 88 µs | 61.5k, p99 15.1 ms, 72 µs | 50.6k, p99 3.7 ms, 46 µs | 64.3k, p99 12.3 ms, 41 µs | | 2 threads | 43.3k, p99 3.9 ms, 43 µs | 46.0k, p99 13.3 ms, 42 µs | 49.0k, p99 3.2 ms, 37 µs | 50.9k, p99 10.9 ms, 37 µs | | **4 threads** | 46.1k, p99 4.3 ms, 61 µs | **74.0k**, p99 10.0 ms, 48 µs | 49.3k, p99 3.7 ms, 47 µs | 64.2k, p99 12.3 ms, 41 µs | On 4 CPUs, 4 threads is Go's default, so that row equals no limit up to noise. At 64 concurrent on 8 CPUs, the three settings are within run-to-run noise (ranges 40–54k). **Through Node** (route handler returning `ok`, same build), where Node's own ~170–210 µs per request is the limit and no setting caps throughput: | Setting | 8 CPUs, 1 concurrent | 8 CPUs, 64 concurrent | 4 CPUs, 64 concurrent | | --- | --- | --- | --- | | no limit | 4.1k req/s, 104 µs | 6.2k, p99 26 ms, 125 µs | 6.7k, p99 18 ms, 99 µs | | 2 threads | 4.4k, 84 µs | 6.8k, p99 18 ms, 85 µs | 6.3k, p99 20 ms, 91 µs | | **4 threads** | 4.2k, 99 µs | 6.8k, p99 18 ms, 101 µs | 6.6k, p99 18 ms, 100 µs | **Earlier measurement through Node** (2 threads vs no limit, before the per-client upstream transports; Switchyard µs per request, req/s change): | Route | 8 CPUs | 4 CPUs | | --- | --- | --- | | bare Node `http` server, 64 concurrent | 99.9 → 41.3 µs (−59%), +15% | 51.9 → 40.4 µs (−22%), ±0 | | route handler, 1 concurrent | 108.8 → 86.1 µs (−21%), +14% | 108.1 → 94.2 µs (−13%), ±0 | | route handler, 64 concurrent | 131.3 → 88.1 µs (−33%), +10% | 107.0 → 87.8 µs (−18%), +6% | | dynamic page, 64 concurrent | 153.3 → 105.9 µs (−31%), +7% | 108.6 → 100.3 µs (−8%), +1% | Findings that don't change this PR: - #102034's change to how responses from Node are flushed doesn't interact with the limit: it only touches the proxy toward Node, and on that route it's within noise with or without the limit. - On the direct path, Switchyard's CPU per request (37–88 µs) is larger than the Go worker's (11–28 µs), so the limit matters more there than through Node. ### Verification - `gofmt`, `go vet ./...` and `go test -race ./...` in `packages/next/src/server/switchyard`, including the updated `TestLimitsThreadsUnlessGOMAXPROCSIsSet` (Go defaults of 1, 2, 4, 8 and 16, and `GOMAXPROCS` set to a valid number, to 0 and to text). - golangci-lint v2.15.0 with #101792's config: no findings in this PR's lines. Two pre-existing findings in canary's code remain, which #101792 fixes. - The benchmarks above. - Not run: the Switchyard e2e suites (no JS changes). 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Tim Neutkens <6324199+timneutkens@users.noreply.github.com>
Author
Parents
Loading