Skip to content

fix: [engines] buffer request body and set Request.GetBody to support HTTP/2 GOAWAY retries#1020

Open
thinker0 wants to merge 1 commit into
trickstercache:mainfrom
thinker0:fix/engines-http2-goaway-retry
Open

fix: [engines] buffer request body and set Request.GetBody to support HTTP/2 GOAWAY retries#1020
thinker0 wants to merge 1 commit into
trickstercache:mainfrom
thinker0:fix/engines-http2-goaway-retry

Conversation

@thinker0
Copy link
Copy Markdown

Description

This pull request solves the 502 Bad Gateway error on POST/PUT requests when the upstream server returns an HTTP/2 GOAWAY frame (e.g., during keep-alive rotation or rolling updates).

Root cause:
In pkg/proxy/engines/httpproxy.go, upstream requests with bodies (like POST requests) did not define the Go standard library http.Request.GetBody field. When the Go http2.Transport (or http.Transport for reused connections) receives a GOAWAY or connection-level error, it tries to automatically retry the request, but ONLY if Request.GetBody is set. Without GetBody, the transport cannot re-read the request body, aborts the retry, and Trickster proxy returns 502 Bad Gateway (under the log signature nil response from upstream origin with detail http2: Transport: cannot retry err after Request.Body was written; define Request.GetBody to avoid this error).

Solution:
Before calling o.HTTPClient.Do(r) in PrepareFetchReader, we read the request body into a memory buffer and set r.GetBody to return a new io.ReadCloser reading from that buffer. This allows Go's HTTP client/transport to clone and re-read the body during transient network/HTTP2 connection failure retries.

Also added clean unit tests (TestPrepareFetchReader_GetBody and TestHTTP2GOAWAYRetry in httpproxy_test.go) to verify body replication and the end-to-end transport retry flow.

Type of Change

  • Bug fix
  • New feature
  • Optimization
  • Test coverage
  • Documentation
  • Infrastructure

AI Disclosure

  • This contribution DOES include AI-generated changes, and I have reviewed the relevant contributing guidelines.

… HTTP/2 GOAWAY retries

Signed-off-by: thinker0 <thinker0@gmail.com>
@thinker0 thinker0 requested a review from a team as a code owner May 29, 2026 06:25
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.

1 participant