-
Notifications
You must be signed in to change notification settings - Fork 104
Add cross-process TPM rate limiter #2855
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
Merged
Dominic Nguyen (datduyng)
merged 14 commits into
main
from
domnguyen/tb-tpm-rate-limiter
Aug 13, 2026
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
463e765
feat(benchmarks): add cross-process TPM rate limiter
datduyng 61220e2
docs: regenerate README.AUTOGEN.md, command reference, and action bro…
typeagent-bot[bot] edd37b5
Add translation-bench run config loader (#2856)
datduyng f8f3538
docs: regenerate README.AUTOGEN.md, command reference, and action bro…
typeagent-bot[bot] 8b55a7d
refactor(benchmarks): split resolveRunConfig to pass complexity cap
datduyng a3aa699
Merge branch 'main' into domnguyen/tb-tpm-rate-limiter
datduyng 8e859ef
chore: requeue CI after flaky infra
datduyng 718a80a
chore: requeue CI after flaky keytar install
datduyng c069202
Merge remote-tracking branch 'origin/main' into _heal
typeagent-bot[bot] 7fa6e3e
docs: regenerate README.AUTOGEN.md, command reference, and action bro…
typeagent-bot[bot] 0ebfc9f
Merge branch 'main' into domnguyen/tb-tpm-rate-limiter
datduyng d9cf714
Merge remote-tracking branch 'origin/main' into _heal
typeagent-bot[bot] 1bcb4ef
docs: regenerate README.AUTOGEN.md, command reference, and action bro…
typeagent-bot[bot] a8d834b
chore: re-trigger CI for #2855
datduyng File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,318 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| import fs from "node:fs"; | ||
| import path from "node:path"; | ||
| import { randomUUID } from "node:crypto"; | ||
| import { DatabaseSync, type StatementSync } from "node:sqlite"; | ||
|
|
||
| const WINDOW_MS = 60_000; | ||
| const MAX_SLEEP_MS = 1_000; | ||
| // Long enough for multi-minute TB translates + retries; pending claims older | ||
| // than this are treated as abandoned (process crash) and purged. | ||
| const STALE_MS = 30 * 60_000; | ||
| const BUSY_TIMEOUT_MS = 15_000; | ||
| const SQLITE_BUSY = 5; | ||
| const OPEN_MAX_ATTEMPTS = 50; | ||
| const OPEN_RETRY_MIN_MS = 20; | ||
| const OPEN_RETRY_JITTER_MS = 30; | ||
|
|
||
| export interface RateLimiterOptions { | ||
| dbPath: string; | ||
| estTokensPerCall?: number; | ||
| maxWaitMs?: number; | ||
| onWait?: (model: string, waitedMs: number, waitMs: number) => void; | ||
| } | ||
|
|
||
| export interface RateLimiter { | ||
| disabledFor(model: string): boolean; | ||
| run<T>( | ||
| model: string, | ||
| est: number | undefined, | ||
| fn: () => Promise<{ result: T; actualTokens: number | undefined }>, | ||
| ): Promise<T>; | ||
| close(): void; | ||
| } | ||
|
|
||
| export type TpmLimits = Readonly<Record<string, number>>; | ||
|
|
||
| interface Reservation { | ||
| id: string | undefined; | ||
| waitMs: number; | ||
| } | ||
|
|
||
| interface ClaimRow { | ||
| created_at: number; | ||
| tokens: number; | ||
| } | ||
|
|
||
| function sleep(ms: number): Promise<void> { | ||
| return new Promise((resolve) => setTimeout(resolve, ms)); | ||
| } | ||
|
|
||
| function isBusyError(error: unknown): boolean { | ||
| return ( | ||
| typeof error === "object" && | ||
| error !== null && | ||
| (error as { errcode?: number }).errcode === SQLITE_BUSY | ||
| ); | ||
| } | ||
|
|
||
| function openDatabase(dbPath: string): DatabaseSync { | ||
| fs.mkdirSync(path.dirname(dbPath), { recursive: true }); | ||
| let lastError: unknown; | ||
| for (let attempt = 0; attempt < OPEN_MAX_ATTEMPTS; attempt++) { | ||
| let db: DatabaseSync | undefined; | ||
| try { | ||
| db = new DatabaseSync(dbPath); | ||
| db.exec(`PRAGMA busy_timeout = ${BUSY_TIMEOUT_MS}`); | ||
| db.exec("PRAGMA journal_mode = WAL"); | ||
| db.exec("PRAGMA synchronous = NORMAL"); | ||
| db.exec( | ||
| "CREATE TABLE IF NOT EXISTS claims (" + | ||
| "id TEXT PRIMARY KEY, " + | ||
| "model TEXT NOT NULL, " + | ||
| "tokens REAL NOT NULL, " + | ||
| "created_at INTEGER NOT NULL, " + | ||
| "pending INTEGER NOT NULL)", | ||
| ); | ||
| db.exec( | ||
| "CREATE INDEX IF NOT EXISTS idx_claims_model_time " + | ||
| "ON claims (model, created_at)", | ||
| ); | ||
| return db; | ||
| } catch (error) { | ||
| lastError = error; | ||
| if (db !== undefined) { | ||
| try { | ||
| db.close(); | ||
| } catch { | ||
| // no-op | ||
| } | ||
| } | ||
| if (!isBusyError(error)) { | ||
| throw error; | ||
| } | ||
| const until = | ||
| Date.now() + | ||
| OPEN_RETRY_MIN_MS + | ||
| Math.floor(Math.random() * OPEN_RETRY_JITTER_MS); | ||
| // Yield the event loop instead of a tight spin-wait. | ||
| const sab = new SharedArrayBuffer(4); | ||
| Atomics.wait( | ||
| new Int32Array(sab), | ||
| 0, | ||
| 0, | ||
| Math.max(1, until - Date.now()), | ||
| ); | ||
| } | ||
| } | ||
| throw lastError; | ||
| } | ||
|
|
||
| class Ledger { | ||
| private readonly insertStmt: StatementSync; | ||
| private readonly settleStmt: StatementSync; | ||
| private readonly insertSettledStmt: StatementSync; | ||
| private readonly purgeExpiredStmt: StatementSync; | ||
| private readonly purgeStaleStmt: StatementSync; | ||
| private readonly usedStmt: StatementSync; | ||
| private readonly oldestStmt: StatementSync; | ||
|
|
||
| constructor( | ||
| private readonly db: DatabaseSync, | ||
| private readonly tpmLimits: TpmLimits, | ||
| ) { | ||
| this.insertStmt = db.prepare( | ||
| "INSERT INTO claims (id, model, tokens, created_at, pending) " + | ||
| "VALUES (?, ?, ?, ?, 1)", | ||
| ); | ||
| this.settleStmt = db.prepare( | ||
| "UPDATE claims SET tokens = ?, pending = 0 WHERE id = ?", | ||
| ); | ||
| this.insertSettledStmt = db.prepare( | ||
| "INSERT OR REPLACE INTO claims " + | ||
| "(id, model, tokens, created_at, pending) VALUES (?, ?, ?, ?, 0)", | ||
| ); | ||
| this.purgeExpiredStmt = db.prepare( | ||
| "DELETE FROM claims WHERE pending = 0 AND created_at <= ?", | ||
| ); | ||
| this.purgeStaleStmt = db.prepare( | ||
| "DELETE FROM claims WHERE pending = 1 AND created_at <= ?", | ||
| ); | ||
| this.usedStmt = db.prepare( | ||
| "SELECT COALESCE(SUM(tokens), 0) AS used " + | ||
| "FROM claims WHERE model = ? AND created_at > ?", | ||
| ); | ||
| this.oldestStmt = db.prepare( | ||
| "SELECT created_at, tokens FROM claims " + | ||
| "WHERE model = ? AND created_at > ? ORDER BY created_at ASC", | ||
| ); | ||
| } | ||
|
|
||
| private transaction<T>(fn: () => T): T { | ||
| this.db.exec("BEGIN IMMEDIATE"); | ||
| try { | ||
| const out = fn(); | ||
| this.db.exec("COMMIT"); | ||
| return out; | ||
| } catch (error) { | ||
| try { | ||
| this.db.exec("ROLLBACK"); | ||
| } catch { | ||
| // no-op | ||
| } | ||
| throw error; | ||
| } | ||
| } | ||
|
|
||
| private waitForCapacity( | ||
| model: string, | ||
| limit: number, | ||
| need: number, | ||
| now: number, | ||
| ): number { | ||
| const excess = need - limit; | ||
| let freed = 0; | ||
| const rows = this.oldestStmt.all( | ||
| model, | ||
| now - WINDOW_MS, | ||
| ) as unknown as ClaimRow[]; | ||
| for (const row of rows) { | ||
| freed += row.tokens; | ||
| if (freed >= excess) { | ||
| return Math.max(5, row.created_at + WINDOW_MS - now); | ||
| } | ||
| } | ||
| return Math.max(5, WINDOW_MS); | ||
| } | ||
|
|
||
| reserve(model: string, cost: number): Reservation { | ||
| const limit = this.tpmLimits[model]; | ||
| const need = Math.min(cost, limit); | ||
| return this.transaction(() => { | ||
| const now = Date.now(); | ||
| this.purgeExpiredStmt.run(now - WINDOW_MS); | ||
| this.purgeStaleStmt.run(now - STALE_MS); | ||
| const { used } = this.usedStmt.get(model, now - WINDOW_MS) as { | ||
| used: number; | ||
| }; | ||
| if (used + need <= limit) { | ||
| const id = randomUUID(); | ||
| this.insertStmt.run(id, model, need, now); | ||
| return { id, waitMs: 0 }; | ||
| } | ||
| return { | ||
| id: undefined, | ||
| waitMs: this.waitForCapacity(model, limit, used + need, now), | ||
| }; | ||
| }); | ||
| } | ||
|
|
||
| settle(id: string, model: string, actualCost: number): void { | ||
| this.transaction(() => { | ||
| const result = this.settleStmt.run(actualCost, id); | ||
| if (result.changes === 0) { | ||
| this.insertSettledStmt.run(id, model, actualCost, Date.now()); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| export function createRateLimiter( | ||
| limits: TpmLimits, | ||
| options: RateLimiterOptions, | ||
| ): RateLimiter { | ||
| const tpmLimits: Record<string, number> = {}; | ||
| for (const [model, tpm] of Object.entries(limits)) { | ||
| if (Number.isFinite(tpm) && tpm > 0) { | ||
| tpmLimits[model] = tpm; | ||
| } | ||
| } | ||
|
|
||
| let db: DatabaseSync | undefined; | ||
| let ledger: Ledger | undefined; | ||
| if (Object.keys(tpmLimits).length > 0) { | ||
| db = openDatabase(options.dbPath); | ||
| ledger = new Ledger(db, tpmLimits); | ||
| } | ||
|
|
||
| async function admit(model: string, estCost: number): Promise<string> { | ||
| const activeLedger = ledger as Ledger; | ||
| const startedAt = Date.now(); | ||
| for (;;) { | ||
| const reservation = activeLedger.reserve(model, estCost); | ||
| if (reservation.id !== undefined) { | ||
| return reservation.id; | ||
| } | ||
| const waited = Date.now() - startedAt; | ||
| if ( | ||
| options.maxWaitMs !== undefined && | ||
| waited >= options.maxWaitMs | ||
| ) { | ||
| throw new Error( | ||
| `rate limiter: exceeded max wait ${options.maxWaitMs}ms for ${model}`, | ||
| ); | ||
| } | ||
| options.onWait?.(model, waited, reservation.waitMs); | ||
| await sleep(Math.min(reservation.waitMs, MAX_SLEEP_MS)); | ||
| } | ||
| } | ||
|
|
||
| async function run<T>( | ||
| model: string, | ||
| est: number | undefined, | ||
| fn: () => Promise<{ result: T; actualTokens: number | undefined }>, | ||
| ): Promise<T> { | ||
| if (ledger === undefined || tpmLimits[model] === undefined) { | ||
| return (await fn()).result; | ||
| } | ||
|
|
||
| const estCost = | ||
| est !== undefined && Number.isFinite(est) && est > 0 | ||
| ? est | ||
| : options.estTokensPerCall; | ||
| if (estCost === undefined || !(estCost > 0)) { | ||
| throw new Error( | ||
| `rate limiter: no positive token estimate for ${model}`, | ||
| ); | ||
| } | ||
|
|
||
| const id = await admit(model, estCost); | ||
| let actual = estCost; | ||
| try { | ||
| const out = await fn(); | ||
| actual = | ||
| out.actualTokens !== undefined && | ||
| Number.isFinite(out.actualTokens) && | ||
| out.actualTokens > 0 | ||
| ? out.actualTokens | ||
| : estCost; | ||
| return out.result; | ||
| } finally { | ||
| try { | ||
| (ledger as Ledger).settle(id, model, actual); | ||
| } catch (error) { | ||
| const message = | ||
| error instanceof Error ? error.message : String(error); | ||
| console.error( | ||
| `[rate-limit] settle failed model=${model} id=${id} actual=${actual}: ${message}`, | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return { | ||
| disabledFor(model: string): boolean { | ||
| return tpmLimits[model] === undefined; | ||
| }, | ||
| close(): void { | ||
| if (db !== undefined) { | ||
| db.close(); | ||
| db = undefined; | ||
| ledger = undefined; | ||
| } | ||
| }, | ||
| run, | ||
| }; | ||
| } | ||
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.