-
proxy: snapshot in-flight counts before sorting (least_conn selection O(n)) (#124)
ci/woodpecker/tag/docker Pipeline was successfulreleased this
2026-08-13 19:55:21 +10:00 Why
least_connmirror selection (baseURLAttemptOrder) scaled super-linearly. After rotating the pool by the round-robin cursor itsort.SliceStabled with a comparator that calledinflightCounteron every comparison — and each call did aremoteName+"\x00"+urlconcat plus async.MapLoadOrStorewith a speculativenew(atomic.Int64). So each selection cost O(n·log n) map lookups + allocations, all on the cache-miss/upstream path.How
Snapshot each mirror's in-flight count once, then sort the snapshot — O(n) map loads, zero comparator allocations.
- Add read-only
inflightCount(name, url) int64: plainsync.MapLoad, returns 0 when the gauge is absent (noLoadOrStore, no speculative allocation). least_connbranch builds a{url, count}snapshot via oneinflightCountper rotated URL,sort.SliceStablebycountascending, then extracts the URLs.beginAttempt/endAttemptkeep the create-on-writeinflightCounterpath — they legitimately need to create the gauge.
Numbers (
BenchmarkBaseURLAttemptOrder_LeastConn, Ryzen 7 4700U, best of 3)pool before ns/op after ns/op before allocs after allocs before B/op after B/op 3 ~2516 ~1492 22 8 474 296 8 ~14647 ~3319 131–132 8 2688 568 Allocs are now constant regardless of pool size; pool-8 is ~4.8x faster with ~16x fewer allocations.
Behavior
Unchanged: least-loaded first, RR rotation as the stable tie-break,
round_robinand single-URL paths untouched. Pure internal optimization — no API/schema/DB change. Added a multi-mirror tie-break test asserting all-equal load yields the RR rotation;make test(-race) green, vet/fmt clean.
Reviewed-on: #124
Co-authored-by: unkin-agent unkin-agent@unkin.net
Co-committed-by: unkin-agent unkin-agent@unkin.netDownloads
- Add read-only