← Back to Pull requests

make addTrailerKey linear in the size of the trailer set #2465

Closed
unknown closed this pull request without merging on Oct 12, 2026 07:12 UTC
View on GitHub
unknown unknown commented on Oct 07, 2026 18:00 UTC
Author

Repro: GET with a 64 KiB Trailer field of distinct names, server has ReadBufferSize: 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. addTrailerKey checks membership with isTrailerKey, which scans every name already recorded, so recording n names costs O(n²). Two parse loops hand it attacker-sized input: addTrailers for the Trailer field of a request or a response, and parseTrailer for 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 plain GET. At the default ReadBufferSize a 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 trailerSetMin names 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 a Trailer field repeated per name doesn't rebuild it.

header_timing_test.go, 8 KiB field, before → after:

BenchmarkRequestHeaderReadTrailerAnnouncement         770222 ns/op → 68951 ns/op
BenchmarkRequestReadTrailerSection                    397699 ns/op → 46430 ns/op
BenchmarkRequestHeaderReadTrailerAnnouncementFields    258365 ns/op → 66578 ns/op

The third is the same set split over one Trailer field 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. BenchmarkRequestHeaderRead doesn'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 because trailerSet.add is 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 with trailerSetMin forced to 0 so every add goes through the index. go test -race -shuffle=on ./..., golangci-lint and 25 s each of FuzzRequestReadLimitBody, FuzzResponseReadLimitBody, FuzzHeaderScanner and FuzzRequestReadLimitBodyAllocations are green.

Diff: 341 lines (+255 / -86) across 4 files Reviewer: alhudz Review duration: 6552m
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
0.42
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
0.17
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
60%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
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
Reviewers
Assignees
Unassigned
Labels
risk: low confidence: 60.5% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 42.3% (MEDIUM) · Higher = larger blast radius
42.3%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 60.5% · Lower = rushed or shallow review
60.5%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 16.7% · Safe to merge
16.7%