Repository navigation
fix: cap concurrent batch searches and rate-limit the API - #95
Merged
Merged
Conversation
A batch search keeps the single JS thread busy for seconds (1,000 packages with many findings each took about 12 s warm), and a single search issued while two batches ran took 1.4 s instead of 2 ms. Nothing limited how many batches ran at once or how often requests came in, so one leaked key or a runaway client could keep the API busy for everyone. - At most BATCH_SEARCH_MAX_CONCURRENT (default 2) batch searches run at once; another gets 429 with Retry-After: 5 before any work starts. - A batch whose caller has disconnected is dropped at the next chunk of 20 packages, and its slot is freed. Before, the work ran to the end after the caller (heretix-management gives up at 60 s) was gone. - @fastify/rate-limit per client address: RATE_LIMIT_MAX per minute (default 1200) for everything but /health, and RATE_LIMIT_BATCH_MAX (default 30) for the batch search. 0 turns a limit off. The callers share one address and one key, so these are a backstop; the concurrency cap is the main guard.
2 of 3 tasks
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.
Problem
POST /vulnerabilities/search/batchkeeps Node's single JS thread busy for seconds. Measured on real data (the 1,000 packages with the most stored rows): about 12.7 s warm for 1,000 packages, 63 s the first time, 113 MB of response. While two batches ran, a single search that normally takes 2 ms took 1.4 s.Changes
BATCH_SEARCH_MAX_CONCURRENT(default 2) batch searches run at once. Another gets429withRetry-After: 5before any work starts, so it holds no connection or parsed body while waiting.@fastify/rate-limitper client address:RATE_LIMIT_MAXper minute (default 1200) for everything but/health, andRATE_LIMIT_BATCH_MAX(default 30) for the batch search.0turns a limit off.docs/operations.md(environment variables, why) anddocs/api.md(429).The rate limit is a backstop: heretix-management reaches the API from one address with one shared key, so a per-address limit cannot tell it from a misuser. The concurrency cap is the main protection.
Not in this PR
429yet; it sends batches one after another, so it is only hit when several scans run at once.Test plan
vitest(747 tests),tsc --noEmit,eslint srcRATE_LIMIT_MAX=0,/healthnever limited, concurrent batch gets 429 and the next one is let in, an invalid batch frees its slot429