make addTrailerKey linear in the size of the trailer set #2465
All Vouch review confidence checks passed
Residual risk 0.17 = Change Risk (0.42) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.42 · 341 lines diff across 4 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.349
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #2465"
general
weight: 0.35
alhudz
Repro:
GETwith a 64 KiBTrailerfield of distinct names, server hasReadBufferSize: 65536. No body, no handler work.Expected: about what 64 KiB in an ordinary header field costs, 21.9 µs/request here.
Actual: 48.1 ms/request, so one core serves about 20 requests/second.
addTrailerKeychecks membership withisTrailerKey, which scans every name already recorded, so recording n names costs O(n²). Two parse loops hand it attacker-sized input:addTrailersfor theTrailerfield of a request or a response, andparseTrailerfor every field of a chunked message's trailer section. Both sit in header parsing, ahead of anything a handler does, which makes this pre-authentication CPU amplification reachable with a plainGET. At the defaultReadBufferSizea 3.9 KiB field still costs 307 µs against 2.2 µs. Repeating one name rather than distinct ones costs 0.35 ms for 78 KiB, which puts the cost in the membership scan and not in splitting the field.Fix: both parses index the set once it already holds more than
trailerSetMinnames and look a name up instead of scanning. Below the threshold, which no real message reaches, the path is unchanged. One index covers a whole header parse, so aTrailerfield repeated per name doesn't rebuild it.header_timing_test.go, 8 KiB field, before → after:The third is the same set split over one
Trailerfield per name, and goes with the shared index.Same request end to end, before → after: 48.1 ms → 0.58 ms at
ReadBufferSize: 65536, and 307 µs → 110 µs for the 3.9 KiB field at the default.BenchmarkRequestHeaderReaddoesn't move, since a message that announces no trailers never reaches the index.The recorded set and its order are what the scan produced. The index is seeded from
h.trailer, lives for the one header parse that owns it, and keys on the set's own stored copy so indexing a name allocates nothing. It stays in step with the set becausetrailerSet.addis the only thing that appends to it once the index exists. I diff-tested the indexed path against the scan over 220k random announcements and trailer sections, and another 240k random multi-field announcements against the shared index, and ran the suite withtrailerSetMinforced to 0 so every add goes through the index.go test -race -shuffle=on ./...,golangci-lintand 25 s each ofFuzzRequestReadLimitBody,FuzzResponseReadLimitBody,FuzzHeaderScannerandFuzzRequestReadLimitBodyAllocationsare green.