-
Notifications
You must be signed in to change notification settings - Fork 140
Refine async payment cache admission #910
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
Open
tnull
wants to merge
1
commit into
lightningdevkit:main
Choose a base branch
from
tnull:2026-05-async-payment-cache-admission
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,8 +14,8 @@ use std::time::{Duration, Instant}; | |
| /// and the max idle duration. | ||
| /// | ||
| /// For every passing of the refill interval, one token is added to the bucket, up to the maximum capacity. When the | ||
| /// bucket has remained at the maximum capacity for longer than the max idle duration, it is removed to prevent memory | ||
| /// leakage. | ||
| /// bucket has remained unused for longer than the max idle duration, it is removed to prevent | ||
| /// memory leakage. | ||
| pub(crate) struct RateLimiter { | ||
| users: HashMap<Vec<u8>, Bucket>, | ||
| capacity: u32, | ||
|
|
@@ -28,6 +28,7 @@ const MAX_USERS: usize = 10_000; | |
| struct Bucket { | ||
| tokens: u32, | ||
| last_refill: Instant, | ||
| last_seen: Instant, | ||
| } | ||
|
|
||
| impl RateLimiter { | ||
|
|
@@ -43,20 +44,22 @@ impl RateLimiter { | |
| if is_new_user { | ||
| self.garbage_collect(self.max_idle); | ||
| if self.users.len() >= MAX_USERS { | ||
| return false; | ||
| self.evict_least_recently_seen(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can now be a rate-limiter bypass for attackers? |
||
| } | ||
| } | ||
|
|
||
| let bucket = self | ||
| .users | ||
| .entry(user_id.to_vec()) | ||
| .or_insert(Bucket { tokens: self.capacity, last_refill: now }); | ||
| let bucket = self.users.entry(user_id.to_vec()).or_insert(Bucket { | ||
| tokens: self.capacity, | ||
| last_refill: now, | ||
| last_seen: now, | ||
| }); | ||
| bucket.last_seen = now; | ||
|
|
||
| let elapsed = now.duration_since(bucket.last_refill); | ||
| let tokens_to_add = (elapsed.as_secs_f64() / self.refill_interval.as_secs_f64()) as u32; | ||
|
|
||
| if tokens_to_add > 0 { | ||
| bucket.tokens = (bucket.tokens + tokens_to_add).min(self.capacity); | ||
| bucket.tokens = bucket.tokens.saturating_add(tokens_to_add).min(self.capacity); | ||
| bucket.last_refill = now; | ||
| } | ||
|
|
||
|
|
@@ -72,7 +75,18 @@ impl RateLimiter { | |
|
|
||
| fn garbage_collect(&mut self, max_idle: Duration) { | ||
| let now = Instant::now(); | ||
| self.users.retain(|_, bucket| now.duration_since(bucket.last_refill) < max_idle); | ||
| self.users.retain(|_, bucket| now.duration_since(bucket.last_seen) < max_idle); | ||
| } | ||
|
|
||
| fn evict_least_recently_seen(&mut self) { | ||
| if let Some(user_to_remove) = self | ||
| .users | ||
| .iter() | ||
| .min_by_key(|(_, bucket)| bucket.last_seen) | ||
| .map(|(user, _)| user.clone()) | ||
| { | ||
| self.users.remove(&user_to_remove); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -99,4 +113,16 @@ mod tests { | |
| assert!(rate_limiter.allow(b"user1")); | ||
| assert!(rate_limiter.allow(b"user2")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn rate_limiter_admits_new_user_at_capacity() { | ||
| let mut rate_limiter = | ||
| RateLimiter::new(3, Duration::from_millis(100), Duration::from_secs(600)); | ||
|
|
||
| for user in 0..super::MAX_USERS { | ||
| assert!(rate_limiter.allow(&user.to_be_bytes())); | ||
| } | ||
|
|
||
| assert!(rate_limiter.allow(b"legit")); | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we talked about this before. Is it really better to allow an attacker to fill up all slots and DoS legit users?