Repository navigation
Conversation
Listing quota policies called GetQuotaPolicyUsage once per row, and each call ran its own database clock query, timezone settings read, and up to four quota-bucket reads. A few hundred policies therefore produced thousands of round trips and made the admin list slow enough to time out. Resolve all rows through one read-only repeatable-read snapshot: one clock and timezone lookup, chunked IN queries for the per-user and per-key buckets, and one grouped aggregate for usage attributed to API keys. The chunk size keeps each statement well below SQLite's and PostgreSQL's parameter limits, so the number of queries stays bounded as the list grows. Store wrappers that override GetQuotaPolicyUsage still receive per-row calls, and a failed batch falls back to the previous per-row behavior so a single bad row cannot hide the others.
Author
|
CI shows I pre-ran the equivalent checks locally at this exact head ( Happy to adjust anything if you'd rather see a different shape. |
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
GET /api/admin/resources/quota-policiesresolves current usage one policy at a time. EachGetQuotaPolicyUsagecall issues its own database clock query, reads the quota timezone setting, then performs up to four bucket reads (day and month, direct and aggregated). With a few hundred policies that is thousands of round trips, and the admin list becomes slow enough to time out on a busy deployment.This change resolves the whole list through one read-only snapshot with a bounded number of queries.
Related Issue
N/A
Changes
readQuotaPolicyUsages, which resolves many quota-policy targets at once.IN (...)queries, chunked at 400 IDs so statements stay below SQLite's and PostgreSQL's parameter limits.all_usersand empty scope IDs stay unsupported, and counters merge by max.Storeimplementations that overrideGetQuotaPolicyUsageworking: the batch path only applies to*GormStore, and every other store keeps per-target calls.Type of Change
Verification
go build ./...passes.go vet ./internal/serverpasses.go test ./internal/server -run 'TestQuotaPolicy|TestUserQuota|TestDefaultUserQuota|TestQuotaTimezone|TestAdminResources' -count=1passes.TestQuotaPolicyUsageBatchMatchesSingleLookups(parity withGetQuotaPolicyUsagefor 4,807 targets and a query-count ceiling),TestQuotaPolicyUsageBatchPropagatesCancellation,TestQuotaPolicyUsageBatchHonorsStoreOverrides.gofmt -lreports no files;git diff --checkpasses.Compatibility, Security, and Operations
current_usagefield is populated.repeatableReadread-only transaction, consistent with the existing usage and billing snapshot reads. A long policy list therefore observes one consistent snapshot instead of a shifting one.Checklist
.envfiles, databases, backups, or runtime logs are included.start.sh, and deployment documentation where applicable.data/model-catalog.yamlremains tracked and catalog changes were reviewed where applicable.git diff --checkpasses.