feat(core): add QuotaManager for unified in-memory quota cache#20
Open
iceteaSA wants to merge 1 commit into
Open
feat(core): add QuotaManager for unified in-memory quota cache#20iceteaSA wants to merge 1 commit into
iceteaSA wants to merge 1 commit into
Conversation
All quota consumers (routing, commands, background timers) can share a single QuotaManager instance with one in-memory cache. This eliminates: - Redundant API calls via inflight request deduplication - 429 rate-limit cascades via global 60s backoff - Stale quota reads across consumers QuotaManager is purely in-memory with no file I/O. FallbackAccountManager accepts an optional quotaManager in its constructor and syncs writes to it after each refresh. Seeding from persisted account.quota prevents unnecessary API calls when on-disk snapshots are still fresh. Non-breaking: quotaManager is optional in AccountManagerOptions. Consumers that don't use it get the existing behavior unchanged.
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.
Summary
Adds a
QuotaManagerclass inpackages/corethat provides a single in-memory cache for main + fallback account quota data. All consumers share one instance so they see the same cache.Key features
refreshMain()calls return the same promiseisMainStale(),isFallbackStale(id),needsRefresh(requestCount)seedFallbacksFromAccounts()hydrates cache from persistedaccount.quotawithout API callsrefreshMainInBackground()for non-blocking background refreshshouldRefreshOnRequestCount(n)for catching fast quota drops between timer checksIntegration with FallbackAccountManager
FallbackAccountManageraccepts an optionalquotaManagerinAccountManagerOptions. When provided:getUsableFallbackAccounts()andrefreshQuotaForDueAccounts()usequotaManager.isFallbackStale()instead of per-account disk stalenessNon-breaking: the parameter is optional. Existing callers that don't pass it get the existing behavior unchanged.
Motivation
Without a shared cache, each quota consumer (routing, commands, background timers) independently fetches from the usage API, leading to:
QuotaManager solves all three by being the single gateway to the usage API with built-in deduplication and backoff.
Files Changed
packages/core/src/quota-manager.ts— new: QuotaManager class (~300 lines)packages/core/src/accounts.ts—AccountManagerOptions.quotaManager,seedFallbackQuota(), staleness integrationpackages/core/src/index.ts— re-exportTesting
All 214 existing tests pass. QuotaManager is opt-in — no existing behavior changes when
quotaManageris not provided.