SMOODEV-969: Python — share rate-limiter state across fetch() calls#79
Merged
Conversation
Previously `_client.fetch()` reconstructed the SlidingWindowRateLimiter on every invocation, so calls routed through the same FetchBuilder did not actually share their sliding window. This left the Python port at odds with the Rust / Go ports, where the limiter lives on the client. Hoist the limiter onto FetchBuilder: - New `_shared_rate_limiter()` lazily builds one SlidingWindowRateLimiter per builder and caches it. Calling `with_rate_limit` again with new options invalidates the cache so the next fetch() rebuilds. - New `acquire_wait()` method on SlidingWindowRateLimiter mirrors the Rust port's `acquire` loop: it sleeps for the limiter's reported `Try again in N ms` hint and retries until a slot is free. The raise-on-full `acquire()` API is preserved so the low-level `fetch()` entrypoint with `container_options.rate_limit` + `rate_limit_retry` keeps its existing semantics. - `fetch()` grows a keyword-only `rate_limiter` parameter so callers (i.e. FetchBuilder) can inject the shared instance. When supplied, the gated path uses `acquire_wait()` instead of `acquire()`. Tests cover the headline scenarios: 5 sequential builder fetches with max=3 window=1s wait >=1 window for the 4th+5th; 5 concurrent fetches via asyncio.gather all succeed and serialize through the window; swapping options via with_rate_limit resets cached state; two separate builders maintain isolated state.
🦋 Changeset detectedLatest commit: 6aa63ba The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
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
FetchBuilderso everyfetch()call through the same builder shares state, matching the Rust / Go ports.SlidingWindowRateLimiter.acquire_wait()mirrors Rust'sacquireloop — sleeps until a slot is free instead of raising. Used automatically when the limiter is builder-owned.fetch()grows a keyword-onlyrate_limiterparameter for injection; existingcontainer_options.rate_limit+rate_limit_retrycallers are unchanged (still get raise-on-full semantics).Test plan
uv run pytest tests/— 126/126 passing, including 5 new tests underTestSharedRateLimitState:asyncio.gatherall succeed and serialize through the windowacquire_wait()blocks instead of raisingwith_rate_limitre-invocation invalidates cached stateuv run ruff check+format --checkcleanbasedpyright— same 6 pre-existing errors on main, zero new🤖 Generated with Claude Code