fix: harden relay reliability and billing integrity - #6201
Conversation
|
Caution Review failedThe pull request is closed. ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (115)
WalkthroughChangesThe PR adds an independent streaming first-byte timeout, improves SSE error and completion handling, strengthens retry/channel selection, applies shared 2FA gates, enforces usable pricing and token groups, introduces atomic subscription-aware task settlement, and improves provider response cleanup and error normalization. Core relay and streaming behavior
Estimated code review effort: 5 (Critical) | ~120 minutes Possibly related issues
Possibly related PRs
Suggested reviewers: Poem
✨ 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 |
Note
Supersedes #6200, which was closed before the review-fix commit could be pushed. All 14 inline findings, 2 outside-diff findings, 4 nitpicks, and the Docstring Coverage warning are addressed in 518d4d6.
503 upstream busyerrors and several accounting/security inconsistencies: - Harden relay failover with bounded exponential jitter,Retry-After, failed-channel exclusion, affinity invalidation, multi-key rotation, original upstream status preservation, and an optional first-byte timeout. - Treat empty streams, premature EOF, malformed chunks, and HTTP 200 business-error payloads as retryable upstream failures before real model output; after output begins, emit protocol-correct OpenAI Chat, Responses, Claude, or Gemini error events without mixing channels. - Close response bodies on all read/error paths and replace unimplemented adaptor panics with explicit errors. - Keep scoped subscription billing inside the purchase-time group snapshot, make subscription refunds atomic/idempotent, validate task usage, snapshot async-video billing ratios, and prevent duplicate Suno refunds/used-quota drift. - Enforce external-login 2FA and disabled-user gates, validate token groups, redact channel details from user logs, and filter Pricing groups by actual availability. - Preserve xAI cached-token usage, Advanced Custominclude_usage, Codex SSE detection, Ali fractional duration, and Cloudflare embedding payloads. ## 🚀 变更类型 / Type of change - [x] 🐛 Bug 修复 (Bug fix) - [x] ✨ 新功能 (New feature: optionalSTREAMING_FIRST_BYTE_TIMEOUT) - [ ] ⚡ 性能优化 / 重构 (Refactor) - [x] 📝 文档更新 (Documentation) ## 🔗 关联任务 / Related Issue Closes #2989 Closes #3192 Closes #4139 Closes #5049 Closes #6095 Closes #6141 Closes #4541 Closes #6021 Closes #6144 Closes #6149 Closes #6075 Closes #6166 Closes #6172 Closes #5200 Closes #5556 Closes #5816 Closes #4211 Closes #6125 Closes #6175 Closes #5139 Known partial overlap with open single-issue PRs: #4060, #6145, #6082, #6174, #6173, #6090, #6089, #5817, #6091, #4323, and #6176. This PR keeps the fixes together because retry selection, stream write boundaries, and billing funding-source invariants share cross-module tests and behavior. ## ✅ 提交前检查项 / Checklist - [ ] 人工确认: AI-assisted draft; awaiting human/maintainer confirmation. - [ ] 非重复提交: Partial overlaps are explicitly listed above. - [x] Bug fix 说明: Corresponding Issues are linked above. - [x] 变更理解: The changed retry, protocol, funding, and authorization paths were reviewed together with their tests. - [x] 范围聚焦: Changes are limited to the audited relay, billing, task, authorization, and compatibility defects. - [x] 本地验证: Focused package tests, repeated concurrency/timeout tests, andgo vetpass locally. - [x] 安全合规: Staged diff was checked for credentials and sensitive channel data exposure. ## 📸 运行证明 / Proof of Work Passed:text go vet -p 1 ./controller ./model ./service ./relay/... ./dto ./middleware ./setting/... ./types/... go test -p 1 ./controller ./model ./service ./relay/... ./dto ./middleware ./setting/... ./types/... -count=1 go test -p 1 ./relay/channel -run 'TestDoRequest.*FirstByte' -count=10 go test -p 1 ./service -run 'TestUpdateSunoTasksConcurrentFailureRefundsOnce|TestSettleTaskBilling_OldSnapshotKeepsLegacyCompletionRatio|TestRecalculateTaskQuotaByTokens_NormalizesInvalidUsage' -count=50 go test -p 1 ./relay/channel/baidu -run TestBaiduStreamHandlerTreatsIsEndFollowedByEOFAsDone -count=100 git diff --cached --checkgo test -p 1 ./... -count=1passed every buildable package; only the root package setup failed because this checkout does not contain the generatedweb/classic/distrequired by the existinggo:embed. Race tests could not start because the local Go toolchain hasCGO_ENABLED=0. Known architectural follow-up: async task terminal-state CAS and all billing side effects are not yet one durable outbox transaction. This PR adds idempotency/CAS protections for the audited duplicate-refund paths but intentionally avoids a database migration for a generalized settlement ledger.Summary by CodeRabbit
New Features
Bug Fixes
Documentation