Skip to content

refactor: refactor HTTP client for improved context and error handling#13

Merged
appleboy merged 2 commits intomainfrom
lint
Feb 15, 2026
Merged

refactor: refactor HTTP client for improved context and error handling#13
appleboy merged 2 commits intomainfrom
lint

Conversation

@appleboy
Copy link
Owner

  • 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>
Copilot AI review requested due to automatic review settings February 15, 2026 04:10
Copy link
Contributor

Copilot AI left a 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 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)
}
Copy link

Copilot AI Feb 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
- 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>
@appleboy appleboy merged commit 7c05a06 into main Feb 15, 2026
20 checks passed
@appleboy appleboy deleted the lint branch February 15, 2026 04:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants