Add client-side rate limiting#270
Merged
Merged
Conversation
RetryPolicy and CircuitBreaker recover from failures, but nothing shaped the rate of calls, so a flow hammering an external API had no way to stay under a quota. Add a token bucket (smooth rate plus burst), a sliding- window limiter (Cloudflare's O(1) weighted counter) and a leading-edge throttle decorator, each with an injectable clock so they are fully deterministic in tests. Wired through the facade, AC_rate_limit executor command, ac_rate_limit MCP tool and the Script Builder.
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 61 |
| Duplication | 0 |
NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.
|
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
RetryPolicy/CircuitBreakerrecover from failures andwork_queueis FIFO, but nothing shaped the rate of calls — a flow hammering an external API had no way to stay under a quota. This adds the two standard limiters plus a leading-edge throttle, all with an injectable clock so they're deterministic in tests (no real sleeping).TokenBucket(rate, capacity)— smooth rate with burst;try_acquire/acquire(timeout=)/time_until_available/tokens(lazy refill).SlidingWindowLimiter(limit, window_s)— fixed budget per rolling window via Cloudflare's O(1) weighted-counter approximation.throttle(interval_s)— decorator firing the wrapped function at most once per interval (leading edge).Pure stdlib (
threadinglock,timeonly as the default clock); injectableclock=/sleep=; Qt-free. (Existing token buckets inremote_desktop/mcp_serverare hard-clocked server guards, not a general primitive.)Five-layer wiring
je_auto_control/utils/rate_limit/__init__.py+__all__AC_rate_limit(named token-bucket instances, likeAC_circuit_call) →{acquired, tokens, wait}ac_rate_limitTests & docs
test/unit_test/headless/test_rate_limit_batch.py(12 tests with a fake clock: burst, refill, cap, blocking acquire, timeout, sliding-window decay, throttle edges)Lint clean: ruff / pylint / bandit / radon (no function CC > 10).