Skip to content

new token bucket impl #6893

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft

Conversation

alexpyattaev
Copy link

@alexpyattaev alexpyattaev commented Jul 9, 2025

Problem

  • We use external crate for a token bucket which is overkill

Summary of Changes

  • Make our own glorious token bucket

@alexpyattaev alexpyattaev added the noCI Suppress CI on this Pull Request label Jul 9, 2025
@alexpyattaev alexpyattaev requested a review from KirillLykov July 9, 2025 11:10
@alexpyattaev alexpyattaev added CI Pull Request is ready to enter CI and removed noCI Suppress CI on this Pull Request labels Jul 9, 2025
@anza-team anza-team removed the CI Pull Request is ready to enter CI label Jul 9, 2025
@codecov-commenter
Copy link

codecov-commenter commented Jul 9, 2025

Codecov Report

Attention: Patch coverage is 83.89058% with 53 lines in your changes missing coverage. Please review.

Project coverage is 83.2%. Comparing base (8d614dd) to head (58d47a2).
Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff            @@
##           master    #6893     +/-   ##
=========================================
- Coverage    83.2%    83.2%   -0.1%     
=========================================
  Files         856      856             
  Lines      376837   377166    +329     
=========================================
+ Hits       313736   313969    +233     
- Misses      63101    63197     +96     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.


/// Provides rate limiting for multiple source IP addresses
/// on demand. Uses LazyLru under the hood.
pub struct KeyedRateLimiter {
Copy link

@KirillLykov KirillLykov Jul 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't it be Sync+Send if we want to use it in different tasks? I'm not sure if LazyLru is.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably yes, will work on it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in adcf32e The implementation is kinda naive and performance is not amazing, but it is simpler and also accurate (unlike the governor crate based impl)

/// Provides rate limiting for multiple source IP addresses
/// on demand. Uses LazyLru under the hood.
pub struct KeyedRateLimiter {
data: RwLock<LruCache<IpAddr, TokenBucket>>,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, governer is using DashMap<IpAddr, Atomicu64> and i think we do something similar here for the performance sake (depends on when do we need this, of course).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah switching to a sharded hashmap would be the most obvious thing to optimize. Time to fork LazyLRU?

@alexpyattaev alexpyattaev force-pushed the ratelimiter branch 2 times, most recently from 4579fe3 to aa91a89 Compare July 13, 2025 18:04
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.

4 participants