fix(#433): stream multipart uploads without buffering files#505
Open
ManuelReschke wants to merge 1 commit into
Open
fix(#433): stream multipart uploads without buffering files#505ManuelReschke wants to merge 1 commit into
ManuelReschke wants to merge 1 commit into
Conversation
imroc
approved these changes
Jul 13, 2026
imroc
left a comment
Owner
There was a problem hiding this comment.
Approved. This is a well-engineered fix for #433. I verified locally (go build ./... and the full root-package go test ./... — all green) since CI checks are not reported on this fork PR (workflow approval required).
Correctness points confirmed:
- Content-Length accuracy: multipartContentLength counts the multipart framing plus the known file sizes, and uses the same file.ContentType that writeMultipartFormFile applies (middleware.go), so the declared Content-Length exactly matches the streamed bytes for SetFile / SetFileBytes. For SetFileReader (empty Content-Type) it correctly falls back to unknown length -> chunked encoding. No length mismatch on the wire.
- Replayability: GetBody (handleMultiPart) builds a fresh io.Pipe + multipart writer per attempt; SetFile reopens the file and SetFileBytes yields a fresh reader each call, so redirects/retries stay replayable. SetFileReader is correctly marked non-replayable via r.unReplayableBody and guarded by the existing retry check (request.go, errRetryableWithUnReplayableBody).
- Bounded memory: r.Body stays nil (middleware.go, handleMultiPart) and the body streams through the pipe, fixing the large-upload buffering from #433.
- Pipe write errors are propagated via pw.CloseWithError instead of being discarded.
- The 3 new tests (streaming+Content-Length, unknown-size reader, non-replayable retry) pass.
Non-blocking observations:
- An empty SetFile (FileSize==0) falls back to unknown Content-Length because multipartContentLength treats FileSize<=0 as unknown. Acceptable edge case, just noting it.
- The throwaway multipart.NewWriter(io.Discard) in handleMultiPart exists only to derive the boundary + content type; harmless.
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.
This change makes multipart uploads stream directly to the HTTP transport instead of building the complete multipart body in a
bytes.Bufferfirst.io.Pipe, keeping memory usage bounded for large uploads.Content-LengthforSetFileandSetFileBytesby counting multipart framing bytes and adding the known file sizes without reading the file contents into memory.SetFilefor each attempt and inspect only the first 512 bytes when detecting their content type.SetFileReaderbodies as non-replayable, preventing retries from silently sending an exhausted reader.Content-Length behavior
SetFileand non-emptySetFileBytesuploads retain an exactContent-Length.FileSizeandContentTypeset also retain an exactContent-Length.Validation performed: