perf(usage): overlap the pre-inference credit checks - #1044
PierreLeGuen wants to merge 4 commits into
Conversation
The usage middleware ran four database reads back to back on every inference request: per-key spend, staking-farm source, organization balance, then active limits. Only the limit read depends on an earlier step (the staking preflight can rewrite limits), so the other three now run concurrently and the limit is read once the preflight has finished. Half of production traffic is served from cpu02, about 61 ms away from the Postgres leader, where each of these reads costs a full network round trip; overlapping them removes two round-trip waits per request. The credit decision itself moves into UsageCheckResult::evaluate so the middleware and UsageService::check_can_use share one implementation.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Review:
|
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 3e4df6f4d3
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
Review · Status🟩 CompletedIronLoop completed the review and posted it to GitHub. ResultRun detailsAutomatic trigger · attempt 1 of 3 · completed in 2m 32s |
There was a problem hiding this comment.
Review · Summary
🟢 No actionable findings
No new actionable findings identified.
Validation
- ✅ Static behavior review — Traced the concurrent spend, staking-preflight, balance, and post-preflight limit paths through their service and repository implementations.
- ✅ Diff hygiene — The proposed changes contain no whitespace errors.
Review details
- Run:
fd6acf6c-86c7-40c8-a659-8d7df7a5952c - Attempts: 1
- A key over its own spend limit (or a failed spend read) is rejected before any organization work runs, as before the overlap. - The spend limit now travels with the spend read (Option<(limit, read)>) so the per-key gate cannot be skipped by a mismatch between the two. - Organizations that have a staking source re-read their balance after the preflight, so a sync that waited on the NEAR RPC cannot leave the gate evaluating a balance from before usage recorded meanwhile. Organizations without a source keep the overlapped read. - The staking mock now spans the window (start, yield, done) so the ordering test can actually fail; unit tests for UsageCheckResult::evaluate.
|
Addressed the review in 62546da:
Re-ran the credit e2e set locally (120 passed) plus the middleware and usage unit tests, clippy and fmt. |
lloydmak99
left a comment
There was a problem hiding this comment.
Solid, well-scoped perf change: the pre-inference credit checks are re-ordered so the staking-farm preflight and the org balance read overlap via tokio::join!, with the credit decision extracted into UsageCheckResult::evaluate. The extraction is faithful and the ordering/error-precedence invariants are preserved.
- The per-key spend gate is still awaited first (
ports.rs:139-175), so 402api_key_limit_exceededand 500 spend-read failures short-circuit before any RPC/DB work, and it's now type-coupled to its spend read (Option<(i64, F)>) so it can't be silently skipped. get_limitruns after thejoin!, preserving the limit-after-preflight invariant; staking orgs re-read balance after the preflight (ports.rs:210-215), matching prior behavior.- Non-blocking: staking orgs do one extra, discarded balance read from the
join!before re-reading (ports.rs:210-215) — wasted work, not a correctness bug, and called out in the PR body. Fine to leave as a follow-up.
Checks: git diff --check and cargo fmt --check passed; targeted unit tests couldn't build locally (no cc linker in sandbox), but GitHub CI lint/unit/integration/audit/deny passed on head 15b45427 (E2E pending at review time).
Reduces sequential waiting in the pre-inference credit gate while preserving rejection precedence and staking synchronization order.
UsageCheckResult::evaluateshares the existing credit decision between middleware and service callers.This saves one sequential database-call wait for non-staking organizations. It does not reduce their query count. Staking organizations and failed preflights perform one extra discarded balance read, and the overlap can use two pool connections. Pool waits and latency under concurrency should be checked during rollout.
Validation: current-head middleware tests passed (7), service tests matching
usagepassed (42), and GitHub CI including E2E is green. No SQL changes.Related: batched signature writes in #1045 and prepared-statement reuse in #1046.