fix(billing): drop transaction wrapper in recordUsage to relieve pool contention#4494
fix(billing): drop transaction wrapper in recordUsage to relieve pool contention#4494TheodoreSpeaks merged 2 commits intostagingfrom
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub. |
PR SummaryMedium Risk Overview The Reviewed by Cursor Bugbot for commit 938b0a9. Bugbot is set up for automated code reviews on this repo. Configure here. |
Greptile SummaryThis PR removes the
Confidence Score: 3/5The fix addresses a real production problem, but introduces a silent-failure mode where certain counter updates can be permanently lost and callers can no longer detect when the write was only partially successful. The apps/sim/lib/billing/core/usage-log.ts — specifically the catch block and the code path where validEntries is empty but additionalStats is present. Important Files Changed
Sequence DiagramsequenceDiagram
participant Caller
participant recordUsage
participant DB_UsageLog as DB: usage_log
participant DB_UserStats as DB: user_stats
Caller->>recordUsage: recordUsage(params)
alt "validEntries.length > 0"
recordUsage->>DB_UsageLog: INSERT (committed immediately)
DB_UsageLog-->>recordUsage: success
end
recordUsage->>DB_UserStats: UPDATE user_stats
alt UPDATE succeeds (row found)
DB_UserStats-->>recordUsage: "result.length === 1"
recordUsage-->>Caller: void (success)
else UPDATE succeeds but row not found
DB_UserStats-->>recordUsage: "result.length === 0"
Note over recordUsage: logger.warn, counter drifts
recordUsage-->>Caller: void (no error thrown)
else UPDATE throws
DB_UserStats-->>recordUsage: throws Error
Note over recordUsage: catch, logger.error, additionalStats lost
recordUsage-->>Caller: void (no error thrown)
end
Reviews (1): Last reviewed commit: "fix(billing): drop transaction wrapper i..." | Re-trigger Greptile |
Summary
recordUsage()inapps/sim/lib/billing/core/usage-log.tsinto two independent statements (INSERT intousage_log, UPDATE onuser_stats) instead of one wrapped transactionuser_statsrow-lock wait, exhausting the pool and failing unrelated requests withquery_wait_timeoutand connection-terminated errorsusage_logis the source of truth (immutable ledger); if theuser_statsUPDATE fails after a successful INSERT the counter drifts and must be reconciled fromusage_logout-of-band. The "row not found" case now logs a warning instead of throwing (no transaction to roll back)Type of Change
Testing
Tested manually.
bun run lintclean,bun run check:api-validation:strictpasses,tsc --noEmitclean for the touched file. No tests directly coverrecordUsage. Followup: add a periodic reconciler that SUMsusage_logper period and correctsuser_stats.currentPeriodCostto recover from any UPDATE failures.Checklist