feat(router): in-memory rolling-window budget accounting engine (#434)#1130
Open
Defilan wants to merge 1 commit into
Open
feat(router): in-memory rolling-window budget accounting engine (#434)#1130Defilan wants to merge 1 commit into
Defilan wants to merge 1 commit into
Conversation
…lantech#434) Foundation slice of defilantech#434: a self-contained, unit-tested in-memory budget accounting engine for the router proxy. BudgetStore provides Allowed / Charge / Snapshot over rolling windows with multi-scope precedence (team/rule/router), token and USD caps, and an injected clock for deterministic tests. Proxy enforcement (429 + Retry-After), response token parsing, ConfigMap plumbing, the token-budget gauge, and status.budgetUtilization are a follow-up; this lands the accounting core they build on. Refs defilantech#434 Signed-off-by: Christopher Maher <chris@mahercode.io>
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
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
A foundation slice of #434: a self-contained, unit-tested in-memory budget accounting engine for the router proxy (
internal/router/budget.go). This does NOT wire into the request path yet — it lands the accounting core.Why
#434 asks for hierarchical token/dollar budgets. The Gateway dataPlane already enforces budgets; the Proxy dataPlane has none. Full proxy enforcement pulls in response token-parsing (streaming is the hard part), a gauge-name collision, ConfigMap plumbing, and cross-pod status — too much for one clean change. Splitting the deterministic, pure accounting engine out first makes the rest reviewable in isolation.
How
BudgetStore:NewBudgetStore(rules, nowFn)— injected clock for deterministic tests.Allowed(scopeKeys)— reports headroom across all matching budgets; returns(ok, retryAfter, exhaustedName), tightest scope trips first.Charge(scopeKeys, tokens, usd)— atomic (mutex) rolling-window accounting.Snapshot()— per-budget used/utilization for a future status/metrics surface.Rolling windows evict usage older than the window; token and USD caps both enforced; int64 overflow guarded; USD parsed from the decimal cap string.
Scope / follow-up
Deliberately deferred (not in this PR): proxy pre-dispatch enforcement (429 +
Retry-After), response token parsing, ConfigMap plumbing of budgets, the token-budget gauge (a distinct name from the existing latency gauge), andstatus.budgetUtilization. Tracked under #434.Testing
17 unit tests: rolling-window expiry + boundary, token/USD exhaustion (and ordering between them), multi-scope precedence + tightest-trips-first, overflow guard, snapshot math, empty/no-matching scope, retry-after positivity, USD parsing, and a concurrent charge/allowed race test.
go test ./internal/router/...green (pure unit, no cluster).go build,go vetclean.Checklist
Refs #434(partial slice — does not close the parent)AI assistance
Implemented via the Foreman agentic-coding harness (GO) with an explicitly descoped prompt, then verified and finalized by the author. Band-3 disclosure per the project's AI-assisted contribution policy.
Refs #434