refactor(rate-limit): use mutex-protected state for fixed-window limiter#2525
Merged
tusharmath merged 2 commits intomainfrom Mar 11, 2026
Merged
refactor(rate-limit): use mutex-protected state for fixed-window limiter#2525tusharmath merged 2 commits intomainfrom
tusharmath merged 2 commits intomainfrom
Conversation
tusharmath
added a commit
that referenced
this pull request
Mar 13, 2026
…ter (#2525) Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.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
Refactor the tracker rate limiter to use a single mutex-protected state instead of separate atomics, ensuring consistent fixed-window behavior under concurrent access.
Context
The previous implementation maintained
window_startandcountas separate atomics and coordinated resets with compare-and-swap logic. While lock-free, this split-state approach can be harder to reason about for fixed-window correctness and race behavior when many threads check/reset simultaneously.Changes
AtomicU64+AtomicUsizefields with aMutex<State>containing bothwindow_startandcount.check_at(now: u64)helper to centralize window logic and enable deterministic testing.check()to delegate tocheck_at(Utc::now().timestamp() as u64).RateLimiter::newandRateLimiter::check.Key Implementation Details
The limiter now acquires a single mutex lock for each check, then performs window rollover and counter increment atomically within that critical section:
now - window_start >= 60.count >= max_per_minute.This keeps all mutable limiter state transitions in one place and avoids interleaving across independent atomic variables.
Use Cases
check()at the same time.Testing
cargo test -p forge_tracker rate_limitExpected result:
test_rate_limiter_blocks_after_limitpassestest_rate_limiter_resets_on_new_windowpassesLinks
crates/forge_tracker/src/rate_limit.rs