Skip to content

perf(billing): compute usage balances once per limit check DEV-926 - #7637

Merged
platreth merged 4 commits into
mainfrom
hugo/dev-926-optimize-check-exceeded-limit
Sep 28, 2026
Merged

platreth merged 4 commits into
mainfrom
hugo/dev-926-optimize-check-exceeded-limit

Conversation

@platreth

@platreth platreth commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

📣 Summary

Checking whether a project owner is over their plan limit now does less repeated work in some submission paths.

📖 Description

When a submission comes in, the server checks whether the project owner is over their submission or storage limit. Some paths, like imports and backups, worked out the owner's usage more than once for the same check. They now do it once. Which submissions get accepted or refused stays the same.

💭 Notes

  • New check_exceeded_limits(user, usage_types) checks several usage types in one call and computes fresh balances at most once, only when a type gets past its 15-min gate. check_exceeded_limit stays as a thin wrapper for the NLP trackers and LRM 0011.
  • In a web request, @cache_for_request already deduplicated the two calls, so nothing changes there. Outside a request (imports, backups, briefcase) that memo does nothing, and main computes twice.
  • Counters on the 402 path still come from a fresh read, not from the cached balances that decided the 402. Storage can drop (file deletes) without clearing that cache, and storage counters drive attachment auto-delete. test_over_limit_rejection_counts_on_fresh_usage covers it.

👀 Preview steps

Backend only. Watch how many times usage gets computed for one refused submission outside a web request.

  1. ℹ️ have a project whose owner is over their submission limit
  2. submit to it without the request middlewares (like an import does) and get a 402
  3. 🔴 [on main] usage computed 3 times
  4. 🟢 [on PR] computed 2 times, same 402
  5. 🟢 test_check_exceeded_limits_computes_balances_once and test_over_limit_rejection_counts_on_fresh_usage cover it

…ks DEV-926

Add check_exceeded_limits to check multiple usage types in one pass, computing balances lazily and accepting caller-provided balances. The submission rejection path now passes the balances it already computed instead of triggering a second full usage recomputation. check_exceeded_limit becomes a thin wrapper over the plural function.
@platreth platreth self-assigned this Sep 24, 2026
@platreth

Copy link
Copy Markdown
Contributor Author

@greptileai

@greptile-apps

greptile-apps Bot commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

[Critical risk] Refactors billing limit checks that gate submission acceptance.

The PR appears safe to merge; no new actionable issue or outstanding previous finding was identified.

Summary

The PR consolidates submission and storage limit-counter checks so they can share one usage-balance calculation. The latest change replaces a global test-cache clear with deletion of the two relevant gate keys.

Reviews (3) · Last reviewed commit: "test(billing): clear only the limit-chec..."

Comment thread kobo/apps/openrosa/libs/utils/logger_tools.py
Comment thread kobo/apps/stripe/tests/test_stripe_utils.py Outdated
@platreth platreth changed the title perf(billing): reuse computed usage balances in submission limit checks DEV-926 perf(billing): compute usage balances once per limit check DEV-926 Sep 25, 2026
@platreth
platreth marked this pull request as ready for review September 25, 2026 05:23
@platreth
platreth removed the request for review from noliveleger September 25, 2026 05:23
Comment thread kobo/apps/openrosa/apps/api/tests/viewsets/test_xform_submission_api.py Outdated
@Guitlle
Guitlle self-requested a review September 25, 2026 16:58

@Guitlle Guitlle left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@platreth
platreth merged commit 11fbd75 into main Sep 28, 2026
18 checks passed
@platreth
platreth deleted the hugo/dev-926-optimize-check-exceeded-limit branch September 28, 2026 07:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants