Repository navigation
feat(aibridge/x/proxy): capture HTTP bodies for replay - #30398
Conversation
3125c17 to
01dbcc0
Compare
ed49523 to
1885657
Compare
e3e8d44 to
b7a42c3
Compare
647b3dd to
bb0e85a
Compare
| } | ||
|
|
||
| func (w *responseWriter) WriteHeader(code int) { | ||
| // 1xx replies are informational, and 101 carries Connection: Upgrade. |
There was a problem hiding this comment.
nit: Do we need the comment about 101 status code?
| response.Body = http.NoBody | ||
| return &response, newBuffer(ctx, nil) | ||
| } | ||
| if response.Trailer == nil { |
There was a problem hiding this comment.
AFAIK, when capture reads the body to EOF in its own goroutine, the Go HTTP transport writes the trailers into this shared Trailer map during that read. Could that race with a consumer like ReverseProxy?
There was a problem hiding this comment.
Great catch, yes there would be a race.
I removed the eager read and changed buffer to lazy read approach. Buffer now is filled by ReverseProxy reading the request / response body.
Also removed most of the HTTP logic from buffer, the 101 switching protocols check is already done in forwardingHandler.checkRequest also moved duplex enablement to handler.
| func (s *bodySource) Close() error { | ||
| if s.controller != nil && !s.eof.Load() { | ||
| //nolint:errcheck // Deadline interruption is best-effort. | ||
| _ = s.controller.SetReadDeadline(time.Now()) |
There was a problem hiding this comment.
Shouldn't we check the error here? 🤔 IIUC, Stop can run while the client is still sending data, so this deadline is the only thing that unblocks the in-progress body Read. If it fails, could Stop hang?
There was a problem hiding this comment.
I removed this, now original source is read in separate go-routine so Close or Stop will not hang on in-flight read. The separate go-routine worst case will timeout with server configured ReadTimeout.
7bd6e6f to
b88c412
Compare
b88c412 to
065c279
Compare
830e62d to
26b5be9
Compare
ssncferreira
left a comment
There was a problem hiding this comment.
LGTM 👍 just a few questions
| // Close marks unfinished capture as interrupted and closes the source. It | ||
| // does not wait for an in-flight source read, whose bytes are dropped. It | ||
| // returns the source close error, cached for later calls. | ||
| func (b *TeeBuffer) Close() error { |
There was a problem hiding this comment.
IIUC, for requests this doesn't stop an in-flight teeRead, since the source is a NopCloser. If the provider returns an error while the client is still uploading, the gateway relays it and the handler returns, but teeRead may still be reading the client's body after that. Could this be a problem?
There was a problem hiding this comment.
An in-flight teeRead can outlive forwarding, but the original request body is owner by server and HTTP server cleanup interrupts pending reads.
There is one trade off, early termination can result in buffer with partially read body, which may prevent recording, eg. prompt.
If we decide that we need full request body before forwarding, handler could read it. TeeBuffer would still be useful for response.
| return b | ||
| } | ||
|
|
||
| // Wait returns the retained bytes once capture finishes. A nil error means |
There was a problem hiding this comment.
With lazy capture, IIUC Wait only returns once the body is fully read or Close is called. If upstream stops reading early and someone calls Wait before Close, could it block forever?
There was a problem hiding this comment.
Wait should be given a context with a timeout. Even if not handler owns the buffer and is responsible for closing it.
Adds HTTP body capture under
aibridge/x/proxy/replaybody.NewRequestandNewResponsereplace the original body with anio.ReadCloserthat retains read bytes in the returnedTeeBuffer.Request retries replay the retained bytes through
r.GetBody().TeeBuffer.Waitreturns buffered bytes without reading the original source.Enables full duplex so Go’s HTTP/1 server can send an early upstream response without first draining the client’s unfinished upload.
Generated by Coder Agents on behalf of @pawbana.