feat: replace custom rate limiter with governor (GCRA, lock-free) - #110
Merged
Conversation
…oses #98) Replace the Mutex<HashMap<String, Vec<Instant>>> sliding-window implementation with the governor crate (GCRA algorithm): - Lock-free enforcement via atomics — eliminates the TOCTOU race from #82 - O(1) per check — no Vec::retain, no background cleanup task - Configurable burst allowance via new optional `rate_limit_burst` field on AgentPolicy (defaults to rate_limit for full backward compatibility) - Per-key limiters stored in RwLock<HashMap> — read lock for lookup (clones Arc), write lock only when inserting a new key; governor check itself is lock-free - Separate AtomicI64 + AtomicU64 window counter gives O(1) remaining-count for X-RateLimit-Remaining headers without any mutex - All 11 existing rate-limit unit tests pass; adds burst_config_respected test Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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
Mutex<HashMap<String, Vec<Instant>>>sliding-window rate limiter with thegovernorcrate (GCRA algorithm)Vec::retainon every request, no background cleanup taskrate_limit_burstfield onAgentPolicy— defaults torate_limitfor full backward compatibilityConfig (new optional field)
Design
LimiterEntry(one per agent / tool / IP) stored inRwLock<HashMap>— read lock to clone theArc, write lock only when inserting a new key; the governor check itself never takes a lockAtomicI64remaining counter +AtomicU64window-start give O(1)X-RateLimit-Remainingheaders via CAS reset every 60sTest plan
cargo fmt --checkpassescargo clippy -- -D warningspassescargo test --lib— 429 tests pass (includes newburst_config_respectedtest)Closes #98
🤖 Generated with Claude Code