Repository navigation
fix: decrement used_quota when refunding async task quota - #4323
octo-patch wants to merge 1 commit into
Conversation
…tumNous#4211) When an async task fails or is over-charged and triggers a refund, the system correctly returned quota to the user's wallet but forgot to decrease their used_quota. This caused the displayed "total quota" (remaining + used) to inflate by the refunded amount on every refund. Changes: - Add exported model.UpdateUserUsedQuota that adjusts used_quota only, without touching request_count (unlike UpdateUserUsedQuotaAndRequestCount). - RefundTaskQuota: call UpdateUserUsedQuota(-quota) and UpdateChannelUsedQuota(-quota) after restoring the wallet balance. - RecalculateTaskQuota: extend the refund branch (quotaDelta < 0) to call UpdateUserUsedQuota and UpdateChannelUsedQuota with the negative delta, mirroring what the charge branch already does.
WalkthroughThe changes introduce a new function to update user-used quota independently and apply it to task billing refund operations to correctly decrement used quota during refunds, fixing a bug where total quota calculations became inaccurate. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ 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 the current code and only fix it if needed.
Inline comments:
In `@service/task_billing.go`:
- Around line 167-169: The refund path must symmetrically decrement used_quota
without touching request_count: replace the prior pre-charge increment usage by
calling UpdateUserUsedQuota(task.UserId, -quota) and
UpdateChannelUsedQuota(task.ChannelId, -quota) (mirroring
UpdateUserUsedQuotaAndRequestCount for charge) while keeping request_count
unchanged; ensure the early guard if quota == 0 return remains and that you
restore the funding source first, then call UpdateUserUsedQuota and
UpdateChannelUsedQuota so used_quota is decremented after refunds are applied.
🪄 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: eaa20577-551d-4b34-beb0-3249e1bf8986
📒 Files selected for processing (2)
model/user.goservice/task_billing.go
| // 3. 减少已用额度统计 | ||
| model.UpdateUserUsedQuota(task.UserId, -quota) | ||
| model.UpdateChannelUsedQuota(task.ChannelId, -quota) |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
# Find async task pre-charge sites and check they increment used_quota symmetrically.
rg -nP --type=go -C3 '\bDecreaseUserQuota\s*\(|PostConsumeUserSubscriptionDelta\s*\('
echo "---"
rg -nP --type=go -C2 'UpdateUserUsedQuota(AndRequestCount)?\s*\('Repository: QuantumNous/new-api
Length of output: 8366
🏁 Script executed:
# Check if this PR modifies funding_source.go or other pre-charge paths
# Look for git diff or check which files are in the PR
git diff --name-only HEAD~1 HEAD 2>/dev/null | grep -E '(funding_source|quota)\.go' || echo "Unable to check git diff, checking file sizes instead"
# Get context on the billing functions in task_billing.go to understand the complete flow
fd task_billing.go --exec wc -l {} \;
head -200 service/task_billing.go | tail -60Repository: QuantumNous/new-api
Length of output: 1862
Refund correctly decrements used_quota — fix matches issue #4211.
Lines 168-169 properly mirror the pre-charge increment at line 63 (UpdateUserUsedQuotaAndRequestCount). Using UpdateUserUsedQuota with a negative value symmetrically decrements both user and channel used_quota, which resolves the total-inflation bug. request_count is intentionally left unchanged, which is correct behavior for a refund.
The guard at line 161 (if quota == 0 return) and the sequence—restore funding source first (line 161), then restore used_quota (lines 168-169)—are sound.
Note: This PR's changes to task_billing.go maintain internal consistency. However, if the broader codebase has other async pre-charge paths that increase used_quota independently, those sites should also have corresponding refund paths to prevent the used_quota from going negative. Scope this validation to other files if needed.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@service/task_billing.go` around lines 167 - 169, The refund path must
symmetrically decrement used_quota without touching request_count: replace the
prior pre-charge increment usage by calling UpdateUserUsedQuota(task.UserId,
-quota) and UpdateChannelUsedQuota(task.ChannelId, -quota) (mirroring
UpdateUserUsedQuotaAndRequestCount for charge) while keeping request_count
unchanged; ensure the early guard if quota == 0 return remains and that you
restore the funding source first, then call UpdateUserUsedQuota and
UpdateChannelUsedQuota so used_quota is decremented after refunds are applied.
51fdfc5 to
2b6f1df
Compare
Fixes #4211
Problem
When an async task fails (triggering
RefundTaskQuota) or is over-charged (triggeringRecalculateTaskQuotawith a negative delta), the system correctly restores the user's remaining quota (wallet balance) but does not decrementused_quota. Since the frontend displays total quota asremaining + used, every refund inflates the total by the refunded amount.Example from the issue:
Root causes in
service/task_billing.go:RefundTaskQuota: callstaskAdjustFunding(-quota)but never callsUpdateUserUsedQuotaorUpdateChannelUsedQuota.RecalculateTaskQuota: in thequotaDelta > 0(extra charge) branch it correctly callsUpdateUserUsedQuotaAndRequestCount+UpdateChannelUsedQuota, but thequotaDelta < 0(refund) branch had no such calls.Solution
model.UpdateUserUsedQuota(id, quota int)— likeUpdateUserUsedQuotaAndRequestCountbut without incrementingrequest_count, suitable for refund adjustments.RefundTaskQuota: after restoring the wallet, callmodel.UpdateUserUsedQuota(task.UserId, -quota)andmodel.UpdateChannelUsedQuota(task.ChannelId, -quota).RecalculateTaskQuotarefund branch: call the same two functions withquotaDelta(which is negative).Testing
RefundTaskQuotais called.Summary by CodeRabbit
Release Notes