-
Notifications
You must be signed in to change notification settings - Fork 113
Static invoice server #621
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
Merged
tnull
merged 1 commit into
lightningdevkit:develop
from
joostjager:static-invoice-server
Sep 16, 2025
Merged
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
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
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
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// This file is Copyright its original authors, visible in version control history. | ||
// | ||
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or | ||
// http://opensource.org/licenses/MIT>, at your option. You may not use this file except in | ||
// accordance with one or both of these licenses. | ||
|
||
mod rate_limiter; | ||
pub(crate) mod static_invoice_store; |
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 |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// This file is Copyright its original authors, visible in version control history. | ||
// | ||
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or | ||
// http://opensource.org/licenses/MIT>, at your option. You may not use this file except in | ||
// accordance with one or both of these licenses. | ||
|
||
//! [`RateLimiter`] to control the rate of requests from users. | ||
|
||
use std::collections::HashMap; | ||
use std::time::{Duration, Instant}; | ||
|
||
/// Implements a leaky-bucket style rate limiter parameterized by the max capacity of the bucket, the refill interval, | ||
/// 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. | ||
pub(crate) struct RateLimiter { | ||
users: HashMap<Vec<u8>, Bucket>, | ||
capacity: u32, | ||
refill_interval: Duration, | ||
max_idle: Duration, | ||
} | ||
|
||
struct Bucket { | ||
tokens: u32, | ||
last_refill: Instant, | ||
} | ||
|
||
impl RateLimiter { | ||
pub(crate) fn new(capacity: u32, refill_interval: Duration, max_idle: Duration) -> Self { | ||
Self { users: HashMap::new(), capacity, refill_interval, max_idle } | ||
} | ||
|
||
pub(crate) fn allow(&mut self, user_id: &[u8]) -> bool { | ||
let now = Instant::now(); | ||
|
||
let entry = self.users.entry(user_id.to_vec()); | ||
let is_new_user = matches!(entry, std::collections::hash_map::Entry::Vacant(_)); | ||
|
||
let bucket = entry.or_insert(Bucket { tokens: self.capacity, last_refill: 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.last_refill = now; | ||
} | ||
|
||
let allow = if bucket.tokens > 0 { | ||
bucket.tokens -= 1; | ||
true | ||
} else { | ||
false | ||
}; | ||
|
||
// Each time a new user is added, we take the opportunity to clean up old rate limits. | ||
if is_new_user { | ||
self.garbage_collect(self.max_idle); | ||
} | ||
|
||
allow | ||
} | ||
|
||
fn garbage_collect(&mut self, max_idle: Duration) { | ||
let now = Instant::now(); | ||
self.users.retain(|_, bucket| now.duration_since(bucket.last_refill) < max_idle); | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::payment::asynchronous::rate_limiter::RateLimiter; | ||
|
||
use std::time::Duration; | ||
|
||
#[test] | ||
fn rate_limiter_test() { | ||
// Test | ||
let mut rate_limiter = | ||
RateLimiter::new(3, Duration::from_millis(100), Duration::from_secs(1)); | ||
|
||
assert!(rate_limiter.allow(b"user1")); | ||
assert!(rate_limiter.allow(b"user1")); | ||
assert!(rate_limiter.allow(b"user1")); | ||
assert!(!rate_limiter.allow(b"user1")); | ||
assert!(rate_limiter.allow(b"user2")); | ||
|
||
std::thread::sleep(Duration::from_millis(150)); | ||
|
||
assert!(rate_limiter.allow(b"user1")); | ||
assert!(rate_limiter.allow(b"user2")); | ||
} | ||
} |
Oops, something went wrong.
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 need to return
Err(ReplayEvent())
here and below in case of persistence failure to be able to retry?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.
Good point. Also
static_invoice_persisted
shouldn't be called in that case. Fixed here and below.