feat: add cross-process rate-limit coordination for Semantic Scholar - #28
Merged
Conversation
AdaptiveRateLimiter is process-local, so a 'research-mcp repl' next to Claude Desktop's 'research-mcp serve' (plus an ad-hoc script) each respect S2's limit individually but collectively exceed it. SharedAdaptiveRateLimiter is a drop-in that coordinates through a JSON sidecar (~/.cache/research-mcp/rate-limits/<source>.json) guarded by an fcntl lock: last_call and the adaptive interval are shared, so all processes on one machine serialize against one minimum interval and one backoff (a 429 in one slows the others). acquire() reserves its slot under the lock then sleeps outside it, with the blocking lock+IO on a worker thread so a sibling can't stall the loop. State uses wall-clock time (comparable across processes) and atomic tmp+rename writes; a missing/corrupt sidecar recovers to baseline. Opt in for S2 with RESEARCH_MCP_S2_SHARED_RATELIMIT=1 (default off, so the test suite never touches shared ~/.cache state). POSIX only — fcntl; Windows deferred.
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.
What
AdaptiveRateLimiteris process-local, so aresearch-mcp replnext to Claude Desktop'sresearch-mcp serve(plus an ad-hoc script) each respect S2's limit individually but collectively exceed it — the user sees more 429s than they should.SharedAdaptiveRateLimiter(sources/_rate_limit_shared.py) is a drop-in forAdaptiveRateLimiter(sameacquire/record_failure/record_success/current_interval) that coordinates through a JSON sidecar at~/.cache/research-mcp/rate-limits/<source>.json, guarded by anfcntllock:last_calland the adaptiveintervalare shared, so every process on the machine serializes against one minimum interval and one backoff (a 429 seen by one process slows the others).acquire()reserves its slot — writes the time it will fire — while holding the lock, then sleeps outside the lock. The blocking lock + IO runs inasyncio.to_thread, so a sibling process holding the lock can't stall the event loop.time.time()(comparable across processes, unlike the local limiter'smonotonic); atomic tmp+rename writes; a missing/corrupt sidecar recovers to baseline.record_*stay synchronous (they're invoked as backoff callbacks) and best-effort — a lock/IO failure never raises into the request path.Opt in for Semantic Scholar with
RESEARCH_MCP_S2_SHARED_RATELIMIT=1(default off, so the test suite never touches shared~/.cachestate). POSIX only —fcntl; Windows is deferred.Closes #8.
Test plan
ruff check src tests/mypy src— cleanpytest -q --strict-markers— 537 passed, 14 skippedtests/unit/test_shared_rate_limit.py(allstate_dir=tmp_path): constructor validation, base/failure/decay interval through the sidecar, cross-instance interval sharing, two instances serializeacquire()(the Cross-process rate-limit coordination (file-lock or shared state) #8 criterion), corrupt-sidecar recovery, atomic write (no leftover.tmp)RESEARCH_MCP_S2_SHARED_RATELIMIT=1→SharedAdaptiveRateLimiter, unset →AdaptiveRateLimiter) under a tempHOME