fix(relay): set Request.GetBody so the HTTP/2 transport can transparently retry after an upstream stream reset - #6249
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (20)
🚧 Files skipped from review as they are similar to previous changes (7)
WalkthroughChangesUpstream body replay
Estimated code review effort: 4 (Complex) | ~45 minutes Possibly related PRs
Suggested reviewers: Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@relay/common/outbound_body.go`:
- Around line 26-43: Update NewOutboundJSONBody’s getBody closure to create and
return a fresh reader backed by an independent copy of the original body data,
rather than rewinding and reusing storage. Preserve the existing error
propagation and io.ReadCloser return contract so retries and redirects each
receive an independently positioned reader.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 6be0eeda-9a45-45a6-8380-8b75e651d239
📒 Files selected for processing (13)
relay/channel/api_request.gorelay/channel/api_request_getbody_test.gorelay/chat_completions_via_responses.gorelay/claude_handler.gorelay/common/outbound_body.gorelay/common/outbound_body_test.gorelay/common/relay_info.gorelay/compatible_handler.gorelay/embedding_handler.gorelay/gemini_handler.gorelay/image_handler.gorelay/rerank_handler.gorelay/responses_handler.go
There was a problem hiding this comment.
🧹 Nitpick comments (1)
relay/common/outbound_body_test.go (1)
20-26: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueUse
testify/assertfor non-fatal value checks.As per coding guidelines, Go backend tests must use
testify/assertfor non-fatal value checks, reservingtestify/requirestrictly for setup and fatal assertions. Therequire.Equalandrequire.EqualValuescalls throughout this test file act as non-fatal checks for the replay payloads and should be updated. Ensure"github.com/stretchr/testify/assert"is imported if not already present.
relay/common/outbound_body_test.go#L20-L26: Changerequire.EqualValues(t, len(payload), size)andrequire.Equal(t, payload, first)to useassert.relay/common/outbound_body_test.go#L36-L36: Changerequire.Equaltoassert.Equal.relay/common/outbound_body_test.go#L58-L68: Changerequire.Equaltoassert.Equalon lines 58 and 67.relay/common/outbound_body_test.go#L84-L84: Changerequire.Equaltoassert.Equal.relay/common/outbound_body_test.go#L96-L111: Changerequire.Equaltoassert.Equalon lines 96, 101, 106, and 111.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@relay/common/outbound_body_test.go` around lines 20 - 26, In relay/common/outbound_body_test.go, replace the non-fatal require.EqualValues and require.Equal checks throughout the outbound body tests with assert.EqualValues and assert.Equal, including the checks in lines 20-26, 36, 58, 67, 84, 96, 101, 106, and 111; add the testify/assert import if needed, while retaining require for setup and fatal assertions.Source: Coding guidelines
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@relay/common/outbound_body_test.go`:
- Around line 20-26: In relay/common/outbound_body_test.go, replace the
non-fatal require.EqualValues and require.Equal checks throughout the outbound
body tests with assert.EqualValues and assert.Equal, including the checks in
lines 20-26, 36, 58, 67, 84, 96, 101, 106, and 111; add the testify/assert
import if needed, while retaining require for setup and fatal assertions.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 8e0a4352-193b-41c0-b2c7-e1b8eacf3796
📒 Files selected for processing (3)
common/body_storage.gorelay/common/outbound_body.gorelay/common/outbound_body_test.go
🚧 Files skipped from review as they are similar to previous changes (1)
- relay/common/outbound_body.go
|
@Calcium-Ion Could you please review this fix? Recommended for merge: it restores safe HTTP/2 transparent retries by providing independent replayable request bodies through |
…ntly retry after an upstream stream reset
The outbound request body is a type-erased io.Reader over BodyStorage, so
net/http cannot derive Request.GetBody (it only does so for *bytes.Reader,
*bytes.Buffer and *strings.Reader). With GetBody nil, the HTTP/2 transport
cannot transparently retry a request once the body has been written and the
upstream resets the stream with a retryable error (REFUSED_STREAM, or a
connection-level GOAWAY); the relay request then fails with:
http2: Transport: cannot retry err [...] after Request.Body was written;
define Request.GetBody to avoid this error
This affects every relay path that goes through DoApiRequest (chat, claude,
gemini, responses, embedding, image, rerank).
BodyStorage (memory and disk) already implements io.Seeker, so replay support
only needed wiring:
- NewOutboundJSONBody additionally returns a getBody that rewinds the storage
and hands out a fresh non-closing reader. The transport only calls GetBody
after the previous attempt's body has been abandoned, so the rewind cannot
race an in-flight read.
- RelayInfo carries it in the new UpstreamRequestGetBody field, set alongside
UpstreamRequestBodySize by the handlers that build storage-backed bodies.
- applyUpstreamGetBody (symmetric with applyUpstreamContentLength) wires it
into DoApiRequest/DoFormRequest/DoTaskApiRequest, only when req.GetBody is
still nil.
Also remove the hand-rolled GetBody override in DoTaskApiRequest: it returned
the same already-consumed reader, so any transport-level replay would have
silently sent an empty body, and it clobbered the correct snapshot-based
GetBody that net/http derives from the *bytes.Reader bodies the task adaptors
pass in. For non-replayable bodies GetBody now stays nil, so a retry fails
loudly instead of corrupting the request.
Covered by unit tests plus an end-to-end raw-frame HTTP/2 test that resets
the first stream with REFUSED_STREAM after the body is written and asserts
the transport transparently retries with the complete body.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Per the http.Request.GetBody contract ("returns a new copy of Body"),
each call must yield a reader with its own cursor. The previous
implementation rewound and reused the shared BodyStorage, so two
consecutive GetBody readers would interfere with each other, and a
replay could disturb the primary body's offset under extreme transport
timing (e.g. attempt N's body write not yet fully abandoned when the
transport builds attempt N+1).
Instead of snapshotting the payload (an extra copy), add
BodyStorage.NewReader, which returns an independent zero-copy reader:
- memory mode: a fresh bytes.Reader over the same immutable backing
array;
- disk mode: a separate file descriptor over the cache file, so the
transport closing a replayed body only closes that descriptor.
NewOutboundJSONBody's getBody now simply hands out storage.NewReader,
and once the handler releases the storage, GetBody fails with
ErrStorageClosed instead of replaying stale data.
Tests: interleaved reads across two replay readers and the primary
body each observe exactly their own byte stream, for both the memory
and the disk-backed storage; the existing GetBody and HTTP/2 retry
suites still pass (h2 e2e tests flake-free with -count=20).
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
c5952ee to
dabee60
Compare
|
Note GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer. |
|
感谢 @BlockchainHe 提供核心的 本次补全包括:rebase 到最新 |
…s / 84 files) 上游本次内容: - Feat/auto group (QuantumNous#6590):令牌自动分组(含前端 auto-group 编排 UI 与后端组选择) - fix(aws): Bedrock 请求在客户端断连时取消 (QuantumNous#6589) - fix(relay): 设置 Request.GetBody,让 HTTP/2 传输在上游 stream reset 后可透明重试 (QuantumNous#6249) - refactor(relay): 把 replay 元数据移到请求体上(RelayInfo.UpstreamRequestBodySize 移除) 冲突 1 处,纯新增函数相邻,两侧保留: - relay/channel/api_request.go:我方 upstreamRequestIDFromHeaders 与上游 keepUpstreamRedirectResponse
Brings in from QuantumNous/new-api: - fix: 修复兑换码额度精度损失 (QuantumNous#6685) - feat(rate-limit): user critical rate limit for access token and aff transfer routes - fix: test Claude/Gemini endpoints with native request format (QuantumNous#6698) - feat(channels): refine fetched model categorization (QuantumNous#6632) - security: atomic access-token rotation and aff updates (merge commit from fork) - refactor(relay): move replay metadata onto request bodies - fix(relay): set Request.GetBody for transparent HTTP/2 retry (QuantumNous#6249) - Feat/auto group (QuantumNous#6590) - fix(aws): cancel Bedrock requests on client disconnect (QuantumNous#6589) Conflicts resolved: - controller/model.go: adopt upstream's unified group-model loop with FormatMatchingModelName token-limit matching; keep fork's hiddenMappedModels filter inside the loop - model/user.go: keep fork's BatchDeleteUsers; adopt upstream's atomic gorm.Expr-based inviteUser - relay/channel/task/sora/adaptor_test.go: keep both fork's New API Video channel tests and upstream's replayable-body test Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015ZteapnbTWdP52n7wZ6NsN
…ntly retry after an upstream stream reset (QuantumNous#6249) * fix(relay): set Request.GetBody so the HTTP/2 transport can transparently retry after an upstream stream reset The outbound request body is a type-erased io.Reader over BodyStorage, so net/http cannot derive Request.GetBody (it only does so for *bytes.Reader, *bytes.Buffer and *strings.Reader). With GetBody nil, the HTTP/2 transport cannot transparently retry a request once the body has been written and the upstream resets the stream with a retryable error (REFUSED_STREAM, or a connection-level GOAWAY); the relay request then fails with: http2: Transport: cannot retry err [...] after Request.Body was written; define Request.GetBody to avoid this error This affects every relay path that goes through DoApiRequest (chat, claude, gemini, responses, embedding, image, rerank). BodyStorage (memory and disk) already implements io.Seeker, so replay support only needed wiring: - NewOutboundJSONBody additionally returns a getBody that rewinds the storage and hands out a fresh non-closing reader. The transport only calls GetBody after the previous attempt's body has been abandoned, so the rewind cannot race an in-flight read. - RelayInfo carries it in the new UpstreamRequestGetBody field, set alongside UpstreamRequestBodySize by the handlers that build storage-backed bodies. - applyUpstreamGetBody (symmetric with applyUpstreamContentLength) wires it into DoApiRequest/DoFormRequest/DoTaskApiRequest, only when req.GetBody is still nil. Also remove the hand-rolled GetBody override in DoTaskApiRequest: it returned the same already-consumed reader, so any transport-level replay would have silently sent an empty body, and it clobbered the correct snapshot-based GetBody that net/http derives from the *bytes.Reader bodies the task adaptors pass in. For non-replayable bodies GetBody now stays nil, so a retry fails loudly instead of corrupting the request. Covered by unit tests plus an end-to-end raw-frame HTTP/2 test that resets the first stream with REFUSED_STREAM after the body is written and asserts the transport transparently retries with the complete body. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(relay): hand out independent readers from GetBody (address review) Per the http.Request.GetBody contract ("returns a new copy of Body"), each call must yield a reader with its own cursor. The previous implementation rewound and reused the shared BodyStorage, so two consecutive GetBody readers would interfere with each other, and a replay could disturb the primary body's offset under extreme transport timing (e.g. attempt N's body write not yet fully abandoned when the transport builds attempt N+1). Instead of snapshotting the payload (an extra copy), add BodyStorage.NewReader, which returns an independent zero-copy reader: - memory mode: a fresh bytes.Reader over the same immutable backing array; - disk mode: a separate file descriptor over the cache file, so the transport closing a replayed body only closes that descriptor. NewOutboundJSONBody's getBody now simply hands out storage.NewReader, and once the handler releases the storage, GetBody fails with ErrStorageClosed instead of replaying stale data. Tests: interleaved reads across two replay readers and the primary body each observe exactly their own byte stream, for both the memory and the disk-backed storage; the existing GetBody and HTTP/2 retry suites still pass (h2 e2e tests flake-free with -count=20). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(relay): bind replayable metadata on pass-through requests * fix(relay): reset upstream body metadata between channels * test(relay): cover replay across retries and channel attempts * fix(relay): stop following upstream redirects --------- Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
- fix(relay): set Request.GetBody so HTTP/2 can transparently retry after upstream stream reset (QuantumNous#6249) - refactor(relay): move replay metadata onto request bodies (适配 fork: 保留 types.PriceData) - feat(channels): refine fetched model categorization (QuantumNous#6632) - fix: test Claude/Gemini endpoints with native request format (QuantumNous#6698) - feat(rate-limit): add user critical rate limit middleware (access-token / aff-transfer 路由) - fix: 修复兑换码额度精度损失 (QuantumNous#6685) (保留 fork 随机额度/下载/100000 批量特性) 跳过: relaykit 模块重构系列(86ac0f7 等, 按 fork 策略), GitCode 发布 CI 工作流, 已同步的 24 个 cherry-pick 副本
- fix(relay): set Request.GetBody for HTTP/2 transparent retry (QuantumNous#6249) - refactor(relay): move replay metadata onto request bodies - fix: user model concurrency safety (access token rotation, invite counters) - feat(rate-limit): user critical rate limit middleware - fix(ali): stop injecting top_p into requests that omit it - fix: test Claude/Gemini endpoints with native request format - feat(channels): refine fetched model categorization - fix: redemption code quota precision loss - drop sync-release-to-gitcode.yml (kept our deletion)
* fix(relay): set Request.GetBody so the HTTP/2 transport can transparently retry after an upstream stream reset (QuantumNous#6249) * fix(relay): set Request.GetBody so the HTTP/2 transport can transparently retry after an upstream stream reset The outbound request body is a type-erased io.Reader over BodyStorage, so net/http cannot derive Request.GetBody (it only does so for *bytes.Reader, *bytes.Buffer and *strings.Reader). With GetBody nil, the HTTP/2 transport cannot transparently retry a request once the body has been written and the upstream resets the stream with a retryable error (REFUSED_STREAM, or a connection-level GOAWAY); the relay request then fails with: http2: Transport: cannot retry err [...] after Request.Body was written; define Request.GetBody to avoid this error This affects every relay path that goes through DoApiRequest (chat, claude, gemini, responses, embedding, image, rerank). BodyStorage (memory and disk) already implements io.Seeker, so replay support only needed wiring: - NewOutboundJSONBody additionally returns a getBody that rewinds the storage and hands out a fresh non-closing reader. The transport only calls GetBody after the previous attempt's body has been abandoned, so the rewind cannot race an in-flight read. - RelayInfo carries it in the new UpstreamRequestGetBody field, set alongside UpstreamRequestBodySize by the handlers that build storage-backed bodies. - applyUpstreamGetBody (symmetric with applyUpstreamContentLength) wires it into DoApiRequest/DoFormRequest/DoTaskApiRequest, only when req.GetBody is still nil. Also remove the hand-rolled GetBody override in DoTaskApiRequest: it returned the same already-consumed reader, so any transport-level replay would have silently sent an empty body, and it clobbered the correct snapshot-based GetBody that net/http derives from the *bytes.Reader bodies the task adaptors pass in. For non-replayable bodies GetBody now stays nil, so a retry fails loudly instead of corrupting the request. Covered by unit tests plus an end-to-end raw-frame HTTP/2 test that resets the first stream with REFUSED_STREAM after the body is written and asserts the transport transparently retries with the complete body. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(relay): hand out independent readers from GetBody (address review) Per the http.Request.GetBody contract ("returns a new copy of Body"), each call must yield a reader with its own cursor. The previous implementation rewound and reused the shared BodyStorage, so two consecutive GetBody readers would interfere with each other, and a replay could disturb the primary body's offset under extreme transport timing (e.g. attempt N's body write not yet fully abandoned when the transport builds attempt N+1). Instead of snapshotting the payload (an extra copy), add BodyStorage.NewReader, which returns an independent zero-copy reader: - memory mode: a fresh bytes.Reader over the same immutable backing array; - disk mode: a separate file descriptor over the cache file, so the transport closing a replayed body only closes that descriptor. NewOutboundJSONBody's getBody now simply hands out storage.NewReader, and once the handler releases the storage, GetBody fails with ErrStorageClosed instead of replaying stale data. Tests: interleaved reads across two replay readers and the primary body each observe exactly their own byte stream, for both the memory and the disk-backed storage; the existing GetBody and HTTP/2 retry suites still pass (h2 e2e tests flake-free with -count=20). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(relay): bind replayable metadata on pass-through requests * fix(relay): reset upstream body metadata between channels * test(relay): cover replay across retries and channel attempts * fix(relay): stop following upstream redirects --------- Co-authored-by: Claude Fable 5 <noreply@anthropic.com> * refactor(relay): move replay metadata onto request bodies * Merge commit from fork * feat(channels): refine fetched model categorization (QuantumNous#6632) * feat(channels): refine fetched model categorization * fix: channel category * fix: hy3 category * fix: test Claude/Gemini endpoints with native request format (QuantumNous#6698) * feat(rate-limit): add user critical rate limit middleware for access token and aff transfer routes * fix: 修复兑换码额度精度损失 (QuantumNous#6685) * fix: 修复兑换码额度精度损失(QuantumNous#6680) * fix(redemption): guard update data integrity * CI: enhance release synchronization workflow with optional file syncing * fix(ali): stop injecting top_p into requests that omit it (QuantumNous#6674) * fix(channels): classify Qwen TTS models correctly (QuantumNous#6711) * feat(channels): add auto-disable-only channel test mode (QuantumNous#6728) * perf(web): debounce server and large-list searches (QuantumNous#6727) * fix: record reasoning effort consistently in usage logs (QuantumNous#6641) * feat(relay): expose user and group context to parameter overrides (QuantumNous#6534) * fix(ollama): preserve reasoning and tool-call context (QuantumNous#6605) * fix: backend length validation (QuantumNous#5548) * feat(billing): highlight matched conditional multipliers in logs (QuantumNous#6561) * feat(billing): highlight matched conditional multipliers in usage logs * fix(billing): make request rule tracing stable and type-safe * fix(web): require confirmation before rotating access token (QuantumNous#6749) --------- Co-authored-by: Lucas <hepo.lucas@gmail.com> Co-authored-by: Claude Fable 5 <noreply@anthropic.com> Co-authored-by: CaIon <i@caion.me> Co-authored-by: RedwindA <128586631+RedwindA@users.noreply.github.com> Co-authored-by: Seefs <40468931+seefs001@users.noreply.github.com> Co-authored-by: lihu-001 <lihu9048@gmail.com> Co-authored-by: ENCHIGO <38551565+ENCHIGO@users.noreply.github.com>
…ntly retry after an upstream stream reset (QuantumNous#6249) * fix(relay): set Request.GetBody so the HTTP/2 transport can transparently retry after an upstream stream reset The outbound request body is a type-erased io.Reader over BodyStorage, so net/http cannot derive Request.GetBody (it only does so for *bytes.Reader, *bytes.Buffer and *strings.Reader). With GetBody nil, the HTTP/2 transport cannot transparently retry a request once the body has been written and the upstream resets the stream with a retryable error (REFUSED_STREAM, or a connection-level GOAWAY); the relay request then fails with: http2: Transport: cannot retry err [...] after Request.Body was written; define Request.GetBody to avoid this error This affects every relay path that goes through DoApiRequest (chat, claude, gemini, responses, embedding, image, rerank). BodyStorage (memory and disk) already implements io.Seeker, so replay support only needed wiring: - NewOutboundJSONBody additionally returns a getBody that rewinds the storage and hands out a fresh non-closing reader. The transport only calls GetBody after the previous attempt's body has been abandoned, so the rewind cannot race an in-flight read. - RelayInfo carries it in the new UpstreamRequestGetBody field, set alongside UpstreamRequestBodySize by the handlers that build storage-backed bodies. - applyUpstreamGetBody (symmetric with applyUpstreamContentLength) wires it into DoApiRequest/DoFormRequest/DoTaskApiRequest, only when req.GetBody is still nil. Also remove the hand-rolled GetBody override in DoTaskApiRequest: it returned the same already-consumed reader, so any transport-level replay would have silently sent an empty body, and it clobbered the correct snapshot-based GetBody that net/http derives from the *bytes.Reader bodies the task adaptors pass in. For non-replayable bodies GetBody now stays nil, so a retry fails loudly instead of corrupting the request. Covered by unit tests plus an end-to-end raw-frame HTTP/2 test that resets the first stream with REFUSED_STREAM after the body is written and asserts the transport transparently retries with the complete body. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(relay): hand out independent readers from GetBody (address review) Per the http.Request.GetBody contract ("returns a new copy of Body"), each call must yield a reader with its own cursor. The previous implementation rewound and reused the shared BodyStorage, so two consecutive GetBody readers would interfere with each other, and a replay could disturb the primary body's offset under extreme transport timing (e.g. attempt N's body write not yet fully abandoned when the transport builds attempt N+1). Instead of snapshotting the payload (an extra copy), add BodyStorage.NewReader, which returns an independent zero-copy reader: - memory mode: a fresh bytes.Reader over the same immutable backing array; - disk mode: a separate file descriptor over the cache file, so the transport closing a replayed body only closes that descriptor. NewOutboundJSONBody's getBody now simply hands out storage.NewReader, and once the handler releases the storage, GetBody fails with ErrStorageClosed instead of replaying stale data. Tests: interleaved reads across two replay readers and the primary body each observe exactly their own byte stream, for both the memory and the disk-backed storage; the existing GetBody and HTTP/2 retry suites still pass (h2 e2e tests flake-free with -count=20). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(relay): bind replayable metadata on pass-through requests * fix(relay): reset upstream body metadata between channels * test(relay): cover replay across retries and channel attempts * fix(relay): stop following upstream redirects --------- Co-authored-by: Claude Fable 5 <noreply@anthropic.com> (cherry picked from commit d6b5ce9)
* v1.0.0-rc.24: (117 commits) CI: enhance release synchronization workflow with optional file syncing fix: 修复兑换码额度精度损失 (QuantumNous#6685) feat(rate-limit): add user critical rate limit middleware for access token and aff transfer routes fix: test Claude/Gemini endpoints with native request format (QuantumNous#6698) feat(channels): refine fetched model categorization (QuantumNous#6632) Merge commit from fork refactor(relay): move replay metadata onto request bodies fix(relay): set Request.GetBody so the HTTP/2 transport can transparently retry after an upstream stream reset (QuantumNous#6249) Feat/auto group (QuantumNous#6590) fix(aws): cancel Bedrock requests on client disconnect (QuantumNous#6589) fix(billing): harden tiered retry group-switch billing (QuantumNous#6570) fix(billing): settle tiered retries with final group (QuantumNous#6518) feat: deepseek responses api (QuantumNous#6562) style: use text-sm for public header nav links to match other nav components (QuantumNous#6557) fix(oauth): stop treating a foreign window.opener as a bind flow (QuantumNous#6425) fix(relay): preserve multipart image edits for New API channels (QuantumNous#6559) feat(logs): expose stream status to log owners (QuantumNous#6558) feat: support zstd request decompression (QuantumNous#6545) fix: preserve Qwen thinking_budget passthrough (QuantumNous#5836) feat(oidc): 支持自定义 OIDC 登录显示名称 (QuantumNous#6012) ... # Conflicts: # service/text_quota.go # web/src/features/models/components/drawers/model-mutate-drawer.tsx # web/src/features/pricing/components/model-details.tsx # web/src/features/pricing/lib/price.ts
…ntly retry after an upstream stream reset (QuantumNous#6249) * fix(relay): set Request.GetBody so the HTTP/2 transport can transparently retry after an upstream stream reset The outbound request body is a type-erased io.Reader over BodyStorage, so net/http cannot derive Request.GetBody (it only does so for *bytes.Reader, *bytes.Buffer and *strings.Reader). With GetBody nil, the HTTP/2 transport cannot transparently retry a request once the body has been written and the upstream resets the stream with a retryable error (REFUSED_STREAM, or a connection-level GOAWAY); the relay request then fails with: http2: Transport: cannot retry err [...] after Request.Body was written; define Request.GetBody to avoid this error This affects every relay path that goes through DoApiRequest (chat, claude, gemini, responses, embedding, image, rerank). BodyStorage (memory and disk) already implements io.Seeker, so replay support only needed wiring: - NewOutboundJSONBody additionally returns a getBody that rewinds the storage and hands out a fresh non-closing reader. The transport only calls GetBody after the previous attempt's body has been abandoned, so the rewind cannot race an in-flight read. - RelayInfo carries it in the new UpstreamRequestGetBody field, set alongside UpstreamRequestBodySize by the handlers that build storage-backed bodies. - applyUpstreamGetBody (symmetric with applyUpstreamContentLength) wires it into DoApiRequest/DoFormRequest/DoTaskApiRequest, only when req.GetBody is still nil. Also remove the hand-rolled GetBody override in DoTaskApiRequest: it returned the same already-consumed reader, so any transport-level replay would have silently sent an empty body, and it clobbered the correct snapshot-based GetBody that net/http derives from the *bytes.Reader bodies the task adaptors pass in. For non-replayable bodies GetBody now stays nil, so a retry fails loudly instead of corrupting the request. Covered by unit tests plus an end-to-end raw-frame HTTP/2 test that resets the first stream with REFUSED_STREAM after the body is written and asserts the transport transparently retries with the complete body. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(relay): hand out independent readers from GetBody (address review) Per the http.Request.GetBody contract ("returns a new copy of Body"), each call must yield a reader with its own cursor. The previous implementation rewound and reused the shared BodyStorage, so two consecutive GetBody readers would interfere with each other, and a replay could disturb the primary body's offset under extreme transport timing (e.g. attempt N's body write not yet fully abandoned when the transport builds attempt N+1). Instead of snapshotting the payload (an extra copy), add BodyStorage.NewReader, which returns an independent zero-copy reader: - memory mode: a fresh bytes.Reader over the same immutable backing array; - disk mode: a separate file descriptor over the cache file, so the transport closing a replayed body only closes that descriptor. NewOutboundJSONBody's getBody now simply hands out storage.NewReader, and once the handler releases the storage, GetBody fails with ErrStorageClosed instead of replaying stale data. Tests: interleaved reads across two replay readers and the primary body each observe exactly their own byte stream, for both the memory and the disk-backed storage; the existing GetBody and HTTP/2 retry suites still pass (h2 e2e tests flake-free with -count=20). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(relay): bind replayable metadata on pass-through requests * fix(relay): reset upstream body metadata between channels * test(relay): cover replay across retries and channel attempts * fix(relay): stop following upstream redirects --------- Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
…ntly retry after an upstream stream reset (QuantumNous#6249) * fix(relay): set Request.GetBody so the HTTP/2 transport can transparently retry after an upstream stream reset The outbound request body is a type-erased io.Reader over BodyStorage, so net/http cannot derive Request.GetBody (it only does so for *bytes.Reader, *bytes.Buffer and *strings.Reader). With GetBody nil, the HTTP/2 transport cannot transparently retry a request once the body has been written and the upstream resets the stream with a retryable error (REFUSED_STREAM, or a connection-level GOAWAY); the relay request then fails with: http2: Transport: cannot retry err [...] after Request.Body was written; define Request.GetBody to avoid this error This affects every relay path that goes through DoApiRequest (chat, claude, gemini, responses, embedding, image, rerank). BodyStorage (memory and disk) already implements io.Seeker, so replay support only needed wiring: - NewOutboundJSONBody additionally returns a getBody that rewinds the storage and hands out a fresh non-closing reader. The transport only calls GetBody after the previous attempt's body has been abandoned, so the rewind cannot race an in-flight read. - RelayInfo carries it in the new UpstreamRequestGetBody field, set alongside UpstreamRequestBodySize by the handlers that build storage-backed bodies. - applyUpstreamGetBody (symmetric with applyUpstreamContentLength) wires it into DoApiRequest/DoFormRequest/DoTaskApiRequest, only when req.GetBody is still nil. Also remove the hand-rolled GetBody override in DoTaskApiRequest: it returned the same already-consumed reader, so any transport-level replay would have silently sent an empty body, and it clobbered the correct snapshot-based GetBody that net/http derives from the *bytes.Reader bodies the task adaptors pass in. For non-replayable bodies GetBody now stays nil, so a retry fails loudly instead of corrupting the request. Covered by unit tests plus an end-to-end raw-frame HTTP/2 test that resets the first stream with REFUSED_STREAM after the body is written and asserts the transport transparently retries with the complete body. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(relay): hand out independent readers from GetBody (address review) Per the http.Request.GetBody contract ("returns a new copy of Body"), each call must yield a reader with its own cursor. The previous implementation rewound and reused the shared BodyStorage, so two consecutive GetBody readers would interfere with each other, and a replay could disturb the primary body's offset under extreme transport timing (e.g. attempt N's body write not yet fully abandoned when the transport builds attempt N+1). Instead of snapshotting the payload (an extra copy), add BodyStorage.NewReader, which returns an independent zero-copy reader: - memory mode: a fresh bytes.Reader over the same immutable backing array; - disk mode: a separate file descriptor over the cache file, so the transport closing a replayed body only closes that descriptor. NewOutboundJSONBody's getBody now simply hands out storage.NewReader, and once the handler releases the storage, GetBody fails with ErrStorageClosed instead of replaying stale data. Tests: interleaved reads across two replay readers and the primary body each observe exactly their own byte stream, for both the memory and the disk-backed storage; the existing GetBody and HTTP/2 retry suites still pass (h2 e2e tests flake-free with -count=20). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(relay): bind replayable metadata on pass-through requests * fix(relay): reset upstream body metadata between channels * test(relay): cover replay across retries and channel attempts * fix(relay): stop following upstream redirects --------- Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
📝 Description
Note
Maintainer follow-up (2026-08-06): The original
BodyStorage.NewReader/Request.GetBodyimplementation and its two commits remain authored by @BlockchainHe. The branch is now rebased onto the latestmain, with separate maintainer commits that:ContentLengthandGetBodyon pass-through bodies, the Sora opaque fallback, and the Jimeng direct request path;REFUSED_STREAM, gracefulGOAWAY, cross-channel lifecycle, Sora, and redirect regression coverage.Explicit
Content-Lengthon type-erased pass-through bodies is a wire-visible improvement; those HTTP/1.1 requests previously used chunked transfer encoding. Current maintainer verification passedgo test ./relay/... ./common/... -count=1, focused-race, HTTP/2 tests with-count=20, scoped build/vet, and independentrelaykittest/build. A rootgo build ./...in this checkout is blocked only because generatedweb/distassets are absent.The detailed original analysis and proof below are preserved as submitted; the current scope and validation are recorded above.
While relaying to an HTTP/2 upstream, requests started failing with:
Go's transport can transparently retry these resets (
REFUSED_STREAM/ gracefulGOAWAY), but only whenRequest.GetBodyis set. The relay never sets it — the outbound body is a type-erased reader overBodyStorage— so every such reset surfaces to the caller even though a retry would have succeeded. While fixing this I also noticedDoTaskApiRequestinstalls aGetBodythat re-reads an already-consumed reader: a retry there would silently send an empty body, which is worse than failing.The fix hands out independent replay readers from
BodyStorage(a newNewReader()on both the memory and disk implementations — zero-copy in both modes), carries the replay hook onRelayInfonext toUpstreamRequestBodySize, and injects it only when net/http didn't already derive a correctGetBody. The broken override is removed. Unit tests plus a raw-frame HTTP/2 end-to-end test cover both the transparent-retry path and the empty-body regression.Full analysis: problem, fix design, and test coverage
Problem
1. Requests on the main relay paths can never be transparently retried by Go's HTTP/2 transport
Per net/http semantics, the HTTP/2 transport can transparently retry a request that fails with a retry-safe error — a stream-level
RST_STREAM(REFUSED_STREAM)(RFC 9113 §8.7: the upstream guarantees the stream was not processed; common with proxy/CDN-fronted upstreams under load or during graceful restarts) or a graceful connection-levelGOAWAY. However, once the request body has been written, retrying requiresRequest.GetBodyto be non-nil; otherwise the request fails outright:The outbound body built by
DoApiRequestis a type-erasedio.Reader(common.ReaderOnly(BodyStorage)), andnet/httponly auto-derivesGetBodyfor*bytes.Reader/*bytes.Buffer/*strings.Reader. SoGetBodystaysnilon every path going throughDoApiRequest— chat / claude / gemini / responses / embedding / image / rerank. This is the other half of the same type-erasure issue thatapplyUpstreamContentLengthalready handles forContentLength.2. The hand-rolled
GetBodyinDoTaskApiRequestsilently replays an empty body (worse than failing)This returns the same, already-consumed reader: if the transport ever retries (or a redirect needs to replay the body), the second attempt sends an empty body. Moreover, most task adaptors pass
*bytes.Readerbodies, for whichnet/httphad already derived a correct snapshot-basedGetBody— this code overwrote a correct implementation with a broken one.Fix
BodyStorageretains the full payload for the lifetime of the request (in memory, or in a disk-backed temp file above the configured threshold), so replay support only needed wiring:BodyStoragegains aNewReader() (io.ReadCloser, error)method returning an independent reader per call, as required by theRequest.GetBodycontract ("a new copy of Body"): the memory implementation returns a freshbytes.Readerover the same immutable backing array (zero-copy), and the disk implementation opens a fresh file descriptor (zero-copy, kernel page-cache backed). Independent cursors mean replays can never race an in-flight read.NewOutboundJSONBodynow also returnsgetBody func() (io.ReadCloser, error)backed byNewReader(); closing a replayed body never affects the underlying storage (its lifecycle stays owned by the handler'sdefer closer.Close()), and once the storage is released,GetBodyfails withErrStorageClosedinstead of replaying stale data.RelayInfogains anUpstreamRequestGetBodyfield, set at the same call sites (8 handlers, 9 call sites) and with the same lifecycle as the existingUpstreamRequestBodySize.applyUpstreamGetBodyhelper (symmetric withapplyUpstreamContentLength) wires it intoDoApiRequest/DoFormRequest/DoTaskApiRequest, and only injects whenreq.GetBody == nil— it never overrides a correct auto-derived implementation.DoTaskApiRequestis removed:*bytes.Reader/*bytes.Buffer-style bodies fall back tonet/http's snapshot-basedGetBody, and non-replayable type-erased bodies keepGetBody == nil, so a retry fails loudly instead of silently sending a corrupted (empty) request.Tests
relay/channel/api_request_getbody_test.go), against a raw-frame HTTP/2 test server that reads the full request body, answers the first attempt withRST_STREAM(REFUSED_STREAM), and serves the retried stream normally with 200:TestUpstreamGetBody_HTTP2RetryAfterUpstreamStreamReset: builds the request the same wayDoApiRequestdoes (type-erased body + bothapplyUpstream*helpers) and asserts the transport retries transparently and the server receives a byte-identical body on both attempts;TestUpstreamGetBody_HTTP2CannotRetryWithoutGetBody: negative case reproducing the pre-fixcannot retry err ... after Request.Body was writtenfailure.TestNewOutboundJSONBody_GetBodyReadersAreIndependent(+_DiskStorage): two readers obtained fromGetBodyare read interleaved and each still yields the complete, correct payload — independent cursors verified for both the memory- and disk-backed storage modes.TestApplyUpstreamGetBody_*: injectedGetBodyis non-nil and two consecutive calls both return the full body; an existingGetBodyis never overridden; without a replay source it staysnil.TestNewOutboundJSONBody_*(relay/common/outbound_body_test.go): after the main body is consumed (or partially read),getBodyreplays the full body repeatedly; closing a replayed body does not affect the storage lifecycle.TestDoTaskApiRequest_KeepsReplayableGetBody: regression test for problem 2 — after the request completes,req.GetBodystill returns the full body repeatedly (it returned an empty body before the fix).go build ./...passes;go vetreports no new findings on the touched packages (identical to the main baseline);go test ./relay/... ./common/...all pass; the two HTTP/2 end-to-end tests run flake-free with-count=20.🚀 Type of change
🔗 Related Issue
✅ 提交前检查项 / Checklist
Bug fix, I have filed or linked a corresponding issue, and I am not classifying design trade-offs, expectation mismatches, or misunderstandings as bugs.📸 Proof of Work
Genuine pre-fix reproduction, captured by running the same e2e scenario without the
applyUpstreamGetBodywiring:Summary by CodeRabbit
Bug Fixes
Tests