Repository navigation
perf(quota): scope availability read views to single accounts #18741
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
75e3e2a
e21bee0
0d49cf3
3d3d4a6
656f951
a8129bd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| - **perf(quota):** Scope availability read views to single accounts and prioritize in-memory usage over repeated SQLite snapshot queries to eliminate 14-21s event loop stalls on instances with many connections; the dashboard availability view now prefers fresh provider-limits usage over a stale persisted snapshot, which is only read when no usage is available ([#18741](https://github.com/cheaper-inference/OmniRoute/pull/18741)) — thanks @bufftop25 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| // #18741 — buildConnectionQuotaAvailability seeds the scoped read view from the fresh | ||
| // provider-limits usage BEFORE consulting SQLite snapshots. Pins the precedence (fresh usage | ||
| // wins over a stale snapshot, and the snapshot is not read at all when usage carries quotas) | ||
| // plus the unchanged fallback (no usage -> the snapshot still hydrates the decision). | ||
| import test from "node:test"; | ||
| import assert from "node:assert/strict"; | ||
| import fs from "node:fs"; | ||
| import os from "node:os"; | ||
| import path from "node:path"; | ||
|
|
||
| const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-18741-")); | ||
| process.env.DATA_DIR = TEST_DATA_DIR; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not replace When As per path instructions, “Tests must isolate DATA_DIR and close DB handles.”
|
||
|
|
||
| const core = await import("../../src/lib/db/core.ts"); | ||
| const snapshots = await import("../../src/lib/db/quotaSnapshots.ts"); | ||
| const availability = await import("../../src/lib/usage/availability.ts"); | ||
|
|
||
| const policy = { enabled: true, defaultThresholdPercent: 2, providerWindowDefaults: {} }; | ||
| const future = new Date(Date.now() + 48 * 3600e3).toISOString(); | ||
|
|
||
| test.after(() => { | ||
| core.resetDbInstance(); | ||
| fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| function saveSnapshot(id: string, remainingPercentage: number, exhausted: boolean) { | ||
| snapshots.saveQuotaSnapshot({ | ||
| provider: "codex", | ||
| connection_id: id, | ||
| window_key: "weekly", | ||
| remaining_percentage: remainingPercentage, | ||
| is_exhausted: exhausted ? 1 : 0, | ||
| next_reset_at: future, | ||
| window_duration_ms: 604800000, | ||
| raw_data: "{}", | ||
| }); | ||
| } | ||
|
|
||
| function usageWith(remainingPercentage: number) { | ||
| return { | ||
| fetchedAt: new Date().toISOString(), | ||
| quotas: { weekly: { remainingPercentage, resetAt: future } }, | ||
| modelQuotas: {}, | ||
| }; | ||
| } | ||
|
|
||
| function accountUsable(id: string, usage: ReturnType<typeof usageWith> | undefined) { | ||
| return availability.buildConnectionQuotaAvailability( | ||
| { id, provider: "codex", isActive: true }, | ||
| usage, | ||
| policy | ||
| ).account.usable; | ||
| } | ||
|
|
||
| test("fresh provider-limits usage wins over a stale exhausted snapshot", () => { | ||
| saveSnapshot("conn-a-18741", 0, true); | ||
| assert.equal(accountUsable("conn-a-18741", usageWith(80)), true); | ||
| }); | ||
|
|
||
| test("fresh exhausted usage still blocks over a healthy snapshot", () => { | ||
| saveSnapshot("conn-b-18741", 80, false); | ||
| assert.equal(accountUsable("conn-b-18741", usageWith(0)), false); | ||
| }); | ||
|
|
||
| test("without usage the persisted snapshot still hydrates the decision", () => { | ||
| saveSnapshot("conn-c-18741", 0, true); | ||
| assert.equal(accountUsable("conn-c-18741", undefined), false); | ||
| }); | ||
|
|
||
| test("no usage and no snapshot stays unknown, not exhausted", () => { | ||
| assert.notEqual(accountUsable("conn-d-18741", undefined), false); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import test from "node:test"; | ||
| import assert from "node:assert/strict"; | ||
| import { getQuotaCacheState } from "../../src/domain/quotaCacheState.ts"; | ||
| import { | ||
| getRequestQuotaCacheState, | ||
| withQuotaCacheReadView, | ||
| } from "../../src/domain/quotaCacheReadView.ts"; | ||
|
|
||
| test("a connection-scoped read clones only that connection and keeps mutations private", () => { | ||
| const state = getQuotaCacheState(); | ||
| const saved = new Map(state.cache); | ||
| const original = globalThis.structuredClone; | ||
| let clones = 0; | ||
| try { | ||
| state.cache.clear(); | ||
| for (let i = 0; i < 132; i++) | ||
| state.cache.set(`c${i}`, { | ||
| connectionId: `c${i}`, | ||
| provider: "codex", | ||
| quotas: { session: { remainingPercentage: 80, resetAt: null } }, | ||
| modelQuotas: {}, | ||
| fetchedAt: Date.now(), | ||
| exhausted: false, | ||
| nextResetAt: null, | ||
| }); | ||
| globalThis.structuredClone = ((value: unknown) => { | ||
| clones++; | ||
| return original(value); | ||
| }) as typeof structuredClone; | ||
| withQuotaCacheReadView(() => { | ||
| const local = getRequestQuotaCacheState(); | ||
| local.cache.get("c0")!.quotas.session.remainingPercentage = 0; | ||
| local.cache.delete("c0"); | ||
| assert.equal(local.cache.has("c1"), false); | ||
| }, "c0"); | ||
| assert.equal(clones, 1); | ||
| assert.equal(state.cache.get("c0")!.quotas.session.remainingPercentage, 80); | ||
| assert.equal(state.cache.size, 132); | ||
| } finally { | ||
| globalThis.structuredClone = original; | ||
| state.cache.clear(); | ||
| for (const [id, entry] of saved) state.cache.set(id, entry); | ||
| } | ||
| }); | ||
|
|
||
| test("an unscoped read retains the full isolated snapshot", () => { | ||
| const state = getQuotaCacheState(); | ||
| withQuotaCacheReadView(() => { | ||
| assert.notEqual(getRequestQuotaCacheState().cache, state.cache); | ||
| assert.equal(getRequestQuotaCacheState().cache.size, state.cache.size); | ||
| }); | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Script executed:
Repository: cheaper-inference/OmniRoute
Length of output: 515
Script executed:
Repository: cheaper-inference/OmniRoute
Length of output: 41763
Script executed:
Repository: cheaper-inference/OmniRoute
Length of output: 36836
Script executed:
Repository: cheaper-inference/OmniRoute
Length of output: 39372
Script executed:
Repository: cheaper-inference/OmniRoute
Length of output: 41999
Script executed:
Repository: cheaper-inference/OmniRoute
Length of output: 23470
Script executed:
Repository: cheaper-inference/OmniRoute
Length of output: 14784
Script executed:
Repository: cheaper-inference/OmniRoute
Length of output: 3657
Script executed:
Repository: cheaper-inference/OmniRoute
Length of output: 1833
Fall back to snapshots when supplied quotas are empty.
Lyceum can return
quotas: {}when its balance endpoint has no data. The cache stores that map with a validfetchedAt.seedReadViewthen creates a non-exhausted entry, and both snapshot guards skipgetQuotaSnapshotFetchedAt. An existing exhausted snapshot is not consulted, so the account can returnusable: true.Treat an empty quota map as unavailable before seeding and in both snapshot guards.
Suggested fix
Committable suggestion