Skip to content

Conversation

@sendya
Copy link
Member

@sendya sendya commented Jan 14, 2026

This pull request enhances HTTP response handling, particularly around connection management and error handling when streaming responses. The main improvements include implementing the http.Hijacker interface for ResponseRecorder, better handling of upstream errors for chunked or unknown-length responses, and refactoring related logic in the server handler.

HTTP Response Handling Improvements:

  • Implemented the http.Hijacker interface for ResponseRecorder in pkg/x/http/response_recorder.go, allowing direct access to the underlying network connection when needed. [1] [2]
  • Added the wrapUpstreamError helper function in server/server.go to close client connections gracefully when upstream response errors occur, specifically for chunked or unknown-length bodies.

Server Handler Logic Updates:

  • Refactored error handling in buildHandler to use the new wrapUpstreamError function, ensuring connections are properly closed on copy errors for chunked or unspecified content-length responses.
  • Improved readability and maintainability by introducing the hasChunked variable and updating logging for chunked responses.

Copilot AI review requested due to automatic review settings January 14, 2026 11:25
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 enhances HTTP response handling by implementing connection hijacking support and improving error handling for streaming responses. The changes enable proper cleanup of client connections when upstream errors occur during response streaming.

Changes:

  • Implemented http.Hijacker interface in ResponseRecorder to enable direct connection access
  • Added wrapUpstreamError helper to gracefully close connections on upstream errors
  • Refactored chunked response handling logic for better maintainability

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
pkg/x/http/response_recorder.go Implements Hijack method to support connection hijacking through the wrapped response writer
server/server.go Adds connection cleanup on upstream errors and refactors chunked response detection

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

if hj, ok := r.ResponseWriter.(http.Hijacker); ok {
return hj.Hijack()
}
return nil, nil, http.ErrHijacked
Copy link

Copilot AI Jan 14, 2026

Choose a reason for hiding this comment

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

The error return should be http.ErrNotSupported instead of http.ErrHijacked. http.ErrHijacked indicates the connection was already hijacked, while http.ErrNotSupported correctly indicates the underlying ResponseWriter doesn't implement the Hijacker interface.

Suggested change
return nil, nil, http.ErrHijacked
return nil, nil, http.ErrNotSupported

Copilot uses AI. Check for mistakes.
var _ http.Hijacker = (*ResponseRecorder)(nil)

type ResponseRecorder struct {
http.Hijacker
Copy link

Copilot AI Jan 14, 2026

Choose a reason for hiding this comment

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

Embedding http.Hijacker as an interface field is unnecessary since the Hijack method is explicitly implemented below. This embedded field serves no purpose and should be removed to avoid confusion.

Suggested change
http.Hijacker

Copilot uses AI. Check for mistakes.
// handle copy error
if err != nil && !errors.Is(err, io.EOF) {
// abort ? continue ?
// quickabort ? continue ?
Copy link

Copilot AI Jan 14, 2026

Choose a reason for hiding this comment

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

Corrected spacing in comment from 'quickabort' to 'quick abort'.

Suggested change
// quickabort ? continue ?
// quick abort ? continue ?

Copilot uses AI. Check for mistakes.
@sendya sendya merged commit e8d3907 into main Jan 14, 2026
1 check passed
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