Repository navigation
perf: snapshot *io.SectionReader bodies by shallow copy - #12
Merged
Merged
Conversation
A SectionReader body previously fell through to sectionReaderFor, which Seek-probes the shared body and wraps it in another SectionReader on every attempt. Only its read cursor is mutable (r, base, limit are constant after creation), so a shallow struct copy gives each attempt an independent cursor over the same stateless ReaderAt — same bytes window ([off, limit)), no probes, no extra indirection. Framing is unchanged: NewRequest cannot size a SectionReader, so the body still goes out chunked, as the retry table test asserts. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
There was a problem hiding this comment.
🟢 Approval recommended
The changes are covered by regression tests with no blocking issues identified.
Pull request overview
Optimizes retry snapshots for *io.SectionReader request bodies while preserving framing and replay behavior.
Changes:
- Shallow-copies
SectionReaderstate per retry attempt. - Adds regression coverage for in-flight uploads and chunked framing.
File summaries
| File | Description |
|---|---|
retry_test.go |
Tests retry integrity for section-reader bodies. |
middleware.go |
Adds efficient SectionReader snapshot handling. |
Review details
- Files reviewed: 2/2 changed files
- Comments generated: 0
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Follow-up to #11. A
*io.SectionReaderbody previously fell throughsnapshotReadertosectionReaderFor, which Seek-probes the shared body (3 calls per attempt) and wraps it in anotherSectionReader, making every read go through two indirection layers (view.ReadAt → orig.ReadAt → underlying.ReadAt).io.SectionReader's only mutable field is the read cursoroff—r,base, andlimitare constant after creation, andReadgoes throughs.r.ReadAt(p, s.off). So a shallow struct copy gives each attempt an independent cursor over the same statelessReaderAt, exactly thebytes.Reader/strings.Readeridiom already in the switch. The copy reads[off, limit), the identical windowsectionReaderForwould have constructed — zero probes, zero extra wrapping.The safety assumption (the underlying
ReadAttolerates concurrent calls) is theio.ReaderAtcontract, the same onesectionReaderForalready relies on.Behavior
http.NewRequestcannot size aSectionReader, so the body still goes out chunked.sectionReaderForstill handles types that exposeReaderAt+Seekerwithout being aSectionReader(e.g.*os.File).Testing
Added a
"section reader body"case to theTestRetryBodyNotCorruptedByInFlightUploadtable (64 MiB in-flight-upload corruption repro, asserts chunked framing). Full suite passes with-race.🤖 Generated with Claude Code