Skip to content

Add client-side rate limiting#270

Merged
JE-Chen merged 1 commit into
devfrom
feat/rate-limit
Jun 20, 2026
Merged

Add client-side rate limiting#270
JE-Chen merged 1 commit into
devfrom
feat/rate-limit

Conversation

@JE-Chen

@JE-Chen JE-Chen commented Jun 20, 2026

Copy link
Copy Markdown
Member

Summary

RetryPolicy/CircuitBreaker recover from failures and work_queue is 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 (threading lock, time only as the default clock); injectable clock=/sleep=; Qt-free. (Existing token buckets in remote_desktop/mcp_server are hard-clocked server guards, not a general primitive.)

Five-layer wiring

  • Headless core: je_auto_control/utils/rate_limit/
  • Facade: re-exported from __init__.py + __all__
  • Executor: AC_rate_limit (named token-bucket instances, like AC_circuit_call) → {acquired, tokens, wait}
  • MCP: ac_rate_limit
  • Script Builder: "Rate Limit (Token Bucket)" under Flow

Tests & 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)
  • v62 feature docs (EN + Zh) + toctree registration
  • What's-new sections in all three READMEs

Lint clean: ruff / pylint / bandit / radon (no function CC > 10).

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.
@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 61 complexity · 0 duplication

Metric Results
Complexity 61
Duplication 0

View in Codacy

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.

@JE-Chen
JE-Chen merged commit 8458e86 into dev Jun 20, 2026
16 checks passed
@JE-Chen
JE-Chen deleted the feat/rate-limit branch June 20, 2026 21:15
@sonarqubecloud

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant