fix(batch): meter each sub-item against the rate limit (S-4) - #211
Merged
Conversation
`/v1/batch` accepts up to 20 items and runs each as a full engine op (entity / metric / NL->SQL), but the auth middleware only charged the single HTTP request one rate-limit token. An authenticated tenant could therefore drive up to 20x its per-minute budget, concentrated on the expensive NL path (audit S-4, P3, metering bypass). Add `AuthManager.charge_rate_limit(tenant_key, units)` that debits `units` extra tokens against the same bucket via the existing `check_rate_limit`, and have the batch handler charge `len(requests) - 1` before executing, rejecting the whole batch with 429 if the budget cannot absorb it. Skipped when auth is disabled (no tenant_key), so per-key isolation and the fail-closed core are untouched. Tests: unit for `charge_rate_limit` (within/over/zero budget) and an integration test that a 5-item batch under a 3/min key returns 429. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
DORA Metrics
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
/v1/batchaccepts up to 20 items and runs each as a full engine op (entity / metric / NL→SQL), but the auth middleware only charged the single HTTP request one rate-limit token. An authenticated tenant could drive up to 20× its per-minute budget, concentrated on the expensive NL path (audit S-4, P3, metering bypass).How
AuthManager.charge_rate_limit(tenant_key, units)debitsunitsextra tokens against the same bucket via the existingcheck_rate_limit— no change to the fail-closed core limiter.len(requests) - 1before executing and rejects the whole batch with 429 if the budget can't absorb it.tenant_key), so per-key isolation and auth-disabled paths are untouched.Semantics: a batch of N items costs N tokens; over budget → 429 (retry after the window), matching normal rate-limit behavior. Bundling
/v1/batch(S-6 landed) keeps buckets keyed by a non-secret id.Tests
charge_rate_limitwithin / over / zero-unit budget.ruff+mypyclean.🤖 Generated with Claude Code