-
Notifications
You must be signed in to change notification settings - Fork 6
Fix request body handling in DoExponentialBackoff #89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Previously, DoExponentialBackoff relied on Seek to reset request bodies between retries. This approach failed when bodies didn't implement Seek or when req.GetBody wasn't set (e.g., manually wrapped io.NopCloser), causing "ContentLength=N with Body length 0" errors on retry attempts. The issue occurred because req.Clone() reuses the exhausted body reader without resetting it, leading to empty bodies on subsequent attempts while ContentLength remained set from the original request. Now read the entire request body upfront and create fresh io.Readers for each retry attempt. This ensures: - Body is always available for all retry attempts - ContentLength is correctly set to match the actual body - Works reliably with all io.Reader types, not just seekable ones Added test case with io.NopCloser-wrapped bytes.Buffer that validates both body content and ContentLength matching across retry attempts.
Pull Request Test Coverage Report for Build 20778351413Details
💛 - Coveralls |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR fixes a bug in DoExponentialBackoff where request bodies would fail to be sent correctly on retry attempts. The previous implementation relied on Seek() to reset bodies between retries, which failed for non-seekable readers or when req.GetBody wasn't set, causing "ContentLength=N with Body length 0" errors.
Key changes:
- Modified body handling to read the entire request body upfront and create fresh readers for each retry
- Updated
ContentLengthto match the actual body size on each retry attempt - Added comprehensive test case validating body content and ContentLength across retries with
io.NopCloser-wrapped bodies
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| httputilx/httputilx.go | Replaced Seek-based body reset with upfront body reading and fresh reader creation for each retry |
| httputilx/httputilx_test.go | Added test case to verify request bodies are correctly preserved across retry attempts |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Previously, DoExponentialBackoff relied on Seek to reset request bodies between retries. This approach failed when bodies didn't implement Seek or when req.GetBody wasn't set (e.g., manually wrapped io.NopCloser), causing "ContentLength=N with Body length 0" errors on retry attempts.
The issue occurred because req.Clone() reuses the exhausted body reader without resetting it, leading to empty bodies on subsequent attempts while ContentLength remained set from the original request.
Now read the entire request body upfront and create fresh io.Readers for each retry attempt. This ensures:
Added test case with io.NopCloser-wrapped bytes.Buffer that validates both body content and ContentLength matching across retry attempts.