Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ use crate::io::{
use crate::liquidity::LiquiditySource;
use crate::logger::{log_debug, log_error, log_info, log_trace, LdkLogger, Logger};
use crate::payment::asynchronous::om_mailbox::OnionMessageMailbox;
use crate::payment::asynchronous::static_invoice_store::StaticInvoiceStore;
use crate::payment::asynchronous::static_invoice_store::{
StaticInvoiceStore, StaticInvoiceStoreError,
};
use crate::payment::forwarding_store::{ForwardRecord, ForwardingStore};
use crate::payment::store::{
Channel, PaymentDetails, PaymentDetailsUpdate, PaymentDirection, PaymentKind, PaymentStatus,
Expand Down Expand Up @@ -2518,7 +2520,11 @@ where
Ok(_) => {
self.channel_manager.static_invoice_persisted(invoice_persisted_path);
},
Err(e) => {
Err(StaticInvoiceStoreError::RateLimited) => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Don't we want to skip the rate limiter once we are retrying persistence, so that we don't exit the retry loop because new requests came in?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Added a fixup for this.

// Drop silently: logging each rejected request, even at trace level,
// can cause excessive log output and I/O under sustained load.
},
Err(StaticInvoiceStoreError::Io(e)) => {
log_error!(self.logger, "Failed to persist static invoice: {}", e);
return Err(ReplayEvent());
},
Expand Down Expand Up @@ -2554,7 +2560,11 @@ where
invoice_slot
);
},
Err(e) => {
Err(StaticInvoiceStoreError::RateLimited) => {
// Drop silently: logging each rejected request, even at trace level,
// can cause excessive log output and I/O under sustained load.
},
Err(StaticInvoiceStoreError::Io(e)) => {
log_error!(self.logger, "Failed to retrieve static invoice: {}", e);
return Err(ReplayEvent());
},
Expand Down
14 changes: 14 additions & 0 deletions src/payment/asynchronous/rate_limiter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,24 @@ impl RateLimiter {
allow
}

/// Returns a token consumed by an admitted request that could not be completed.
pub(crate) fn refund(&mut self, user_id: &[u8]) {
if let Some(bucket) = self.users.get_mut(user_id) {
bucket.tokens = bucket.tokens.saturating_add(1).min(self.capacity);
}
}

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)]
pub(super) fn advance_refill_interval(&mut self) {
for bucket in self.users.values_mut() {
bucket.last_refill -= self.refill_interval;
}
}
}

#[cfg(test)]
Expand Down
Loading
Loading