Skip to content

Local rate-limit window: throttle the eviction sweep when the key table is full of live keys #21907

Closed

Description

@mrveiss

Finding (review of PR #21823)

In autobot_shared/rate_limiter_window.py, when the local key table is at LOCAL_WINDOW_MAX_KEYS and every key is still live, each request for a new key scans the whole table under the lock (_live / _evict_stale) before refusing. During a Redis outage in LOCAL mode, a flood of distinct keys makes that scan run on every request, costing event-loop CPU. It is bounded by the cap, so this is hardening, not a defect.

Acceptance criteria

  • The full-table eviction sweep runs at most once per configurable interval (env-backed constant), and new keys are refused without a scan in between
  • A test shows N new-key calls on a full live table trigger at most one sweep per interval
  • autobot_shared/rate_limiter.py stays under 600 lines (it was 599 at review time; split if touched)

Activity

  1. mrveiss commented on Nov 21, 2026

    @mrveiss
    OwnerAuthor

    Closure check: PR #21966 merged at a40a7d4. Each criterion below was verified against origin/main.

    AC Evidence
    Sweep runs at most once per interval, env-backed, and new keys are refused with no scan in between autobot_shared/rate_limiter_window.py:62 defines LOCAL_WINDOW_SWEEP_INTERVAL = env_float_clamped("AUTOBOT_RATE_LIMIT_LOCAL_SWEEP_INTERVAL", 1.0, positive_finite=True). _sweep_if_due (L138-143) returns early while mono - self._last_sweep < self._sweep_interval. _last_sweep is monotonic and only touched under _lock (L91).
    A test shows N new keys trigger at most one sweep per interval autobot_shared/rate_limiter_window_18087_test.py:240 TestSweepThrottle, :253 test_n_new_keys_on_a_full_live_table_sweep_once_per_interval: 50 new keys give 1 scan. With the throttle removed it fails with assert 50 == 1 (shown in #21966).
    rate_limiter.py stays under 600 lines autobot_shared/rate_limiter.py is 599 lines on main and was not touched by #21966.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions