Conversation
Owner
appleboy
commented
Feb 15, 2026
- Replace defer-based file close with explicit file.Close() calls for error handling in large file upload example
- Update tests to use NewRequestWithContext for improved context control
- Refactor retry logic into smaller functions for maintainability and clarity
- Use a single contentTypeJSON constant in place of repeated string literals in tests
- Improve context cancellation handling during retries and response body closing
- Consolidate per-attempt metrics and status recording in HTTP retries
- Replace defer-based file close with explicit file.Close() calls for error handling in large file upload example - Update tests to use NewRequestWithContext for improved context control - Refactor retry logic into smaller functions for maintainability and clarity - Use a single contentTypeJSON constant in place of repeated string literals in tests - Improve context cancellation handling during retries and response body closing - Consolidate per-attempt metrics and status recording in HTTP retries Signed-off-by: appleboy <appleboy.tw@gmail.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refactors the HTTP client implementation to improve code maintainability, context handling, and error handling patterns. The changes focus on extracting retry logic into smaller, more focused functions and updating tests to use more explicit context management.
Changes:
- Refactored retry logic in retry.go into three smaller functions (calculateRetryDelay, executeAttempt, wrapBodyWithCancel) for improved maintainability
- Updated all test files to use http.NewRequestWithContext instead of http.NewRequest for better context control
- Introduced contentTypeJSON constant in retry_test.go to eliminate string literal duplication
- Modified example_test.go to use explicit file.Close() calls instead of defer for clearer error handling
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| retry.go | Refactored DoWithContext retry logic into three smaller helper functions (calculateRetryDelay, executeAttempt, wrapBodyWithCancel) with corresponding result structs for improved code organization and testability |
| tracer_test.go | Updated test to use NewRequestWithContext for improved context control |
| retry_test.go | Added contentTypeJSON constant and replaced all "application/json" string literals; consistent with context-aware request creation |
| observability_test.go | Updated all tests to use NewRequestWithContext for better context management |
| metric_test.go | Updated tests to use NewRequestWithContext and added defensive response body closing for error cases |
| example_test.go | Changed from defer-based file closing to explicit file.Close() calls in error paths for more granular error handling control |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
591
to
593
| if err != nil { | ||
| log.Fatal(err) | ||
| } |
There was a problem hiding this comment.
If client.Do returns an error with a non-nil response, the response body should be closed before calling log.Fatal to avoid resource leaks. Consider adding:
if resp != nil && resp.Body != nil {
resp.Body.Close()
}
before the log.Fatal call.
- Ensure HTTP response bodies are closed after errors to prevent resource leaks - Add cleanup logic for response bodies at all points where requests may fail Signed-off-by: appleboy <appleboy.tw@gmail.com>
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.