Skip to content

Commit

Permalink
Make rate limits configureable.
Browse files Browse the repository at this point in the history
Add 'rate_limit_count' and 'rate_limit_period' to configuration option.
  • Loading branch information
kannapoix committed Oct 15, 2024
1 parent 259de8b commit 8d34e5b
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 9 deletions.
12 changes: 12 additions & 0 deletions config_spec.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,15 @@ name = "response_invoice_timeout"
type = "u32"
optional = true
doc = "Amount of time in seconds that server waits for an offer creator to respond with an invoice. Defaults to 15s."

[[param]]
name = "rate_limit_count"
type = "u8"
default = "10"
doc = "The number of calls each peer is allowed to make within the rate limit period. This value determines the maximum number of requests a peer can send before being rate limited."

[[param]]
name = "rate_limit_period_secs"
type = "u64"
default = "1"
doc = "The duration of the rate limit period in seconds. This value specifies the time window over which the rate limit count is applied."
4 changes: 4 additions & 0 deletions sample-lndk.conf
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ macaroon_path="/home/<USERNAME>/.lnd/data/chain/bitcoin/regtest/admin.macaroon"
# macaroon-hex="0201036C6E6402F801030A1034F41C28A3B5190702FEA607DD4045AE1201301A160A0761646472657373120472656164120577726974651A130A04696E666F120472656164120577726974651A170A08696E766F69636573120472656164120577726974651A210A086D616361726F6F6E120867656E6572617465120472656164120577726974651A160A076D657373616765120472656164120577726974651A170A086F6666636861696E120472656164120577726974651A160A076F6E636861696E120472656164120577726974651A140A057065657273120472656164120577726974651A180A067369676E6572120867656E6572617465120472656164000006205CF3E77A97764FB2A68967832608591BE375B36F51A6269AB425AA767BC22B55"

response_invoice_timeout=15

# Rate limits for onion messaging. Followings are the default values.
# rate_limit_count=1
# rate_limit_period_secs=10
7 changes: 7 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ use log4rs::append::console::ConsoleAppender;
use log4rs::append::file::FileAppender;
use log4rs::config::{Appender, Config as LogConfig, Logger, Root};
use log4rs::encode::pattern::PatternEncoder;
use rate_limit::RateLimiterCfg;
use std::collections::HashMap;
use std::str::FromStr;
use std::sync::{Arc, Mutex, Once};
Expand Down Expand Up @@ -129,6 +130,8 @@ pub struct Cfg {
pub lnd: LndCfg,
pub signals: LifecycleSignals,
pub skip_version_check: bool,
pub rate_limit_count: u8,
pub rate_limit_period_secs: u64,
}

#[derive(Clone)]
Expand Down Expand Up @@ -236,6 +239,10 @@ impl LndkOnionMessenger {
onion_messenger,
network,
args.signals,
RateLimiterCfg {
call_count: args.rate_limit_count,
call_period_secs: Duration::from_secs(args.rate_limit_period_secs),
},
)
.await
}
Expand Down
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ async fn main() -> Result<(), ()> {
lnd: lnd_args,
signals,
skip_version_check: config.skip_version_check,
rate_limit_count: config.rate_limit_count,
rate_limit_period_secs: config.rate_limit_period_secs,

Check warning on line 58 in src/main.rs

View check run for this annotation

Codecov / codecov/patch

src/main.rs#L57-L58

Added lines #L57 - L58 were not covered by tests
};

let response_invoice_timeout = config.response_invoice_timeout;
Expand Down
13 changes: 4 additions & 9 deletions src/onion_messenger.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::clock::TokioClock;
use crate::lnd::{features_support_onion_messages, ONION_MESSAGES_OPTIONAL};
use crate::rate_limit::{RateLimiter, TokenLimiter};
use crate::rate_limit::{RateLimiter, RateLimiterCfg, TokenLimiter};
use crate::{LifecycleSignals, LndkOnionMessenger, LDK_LOGGER_NAME};
use async_trait::async_trait;
use bitcoin::blockdata::constants::ChainHash;
Expand Down Expand Up @@ -46,12 +46,6 @@ const ONION_MESSAGE_TYPE: u32 = 513;
/// MSG_POLL_INTERVAL is the interval at which we poll for outgoing onion messages.
const MSG_POLL_INTERVAL: Duration = Duration::from_millis(100);

/// DEFAULT_CALL_COUNT is the default number of calls each peer gets per rate limited period.
const DEFAULT_CALL_COUNT: u8 = 10;

/// DEFAULT_CALL_FREQUENCY is the default period over which peers are rate limited.
const DEFAULT_CALL_FREQUENCY: Duration = Duration::from_secs(1);

/// Node Id LookUp is a utility struct implementing NodeIdLookUp trait for LDK's OnionMessenger.
pub struct LndkNodeIdLookUp {
client: Client,
Expand Down Expand Up @@ -167,6 +161,7 @@ impl LndkOnionMessenger {
onion_messenger: OnionMessenger<ES, NS, L, NL, MR, OMH, CMH>,
network: Network,
signals: LifecycleSignals,
rate_limiter_cfg: RateLimiterCfg,
) -> Result<(), ()>
where
ES::Target: EntropySource,
Expand Down Expand Up @@ -273,8 +268,8 @@ impl LndkOnionMessenger {
// events we need).
let rate_limiter = &mut TokenLimiter::new(
current_peers.keys().copied(),
DEFAULT_CALL_COUNT,
DEFAULT_CALL_FREQUENCY,
rate_limiter_cfg.call_count,
rate_limiter_cfg.call_period_secs,
TokioClock::new(),
);
let mut message_sender = CustomMessenger {
Expand Down
5 changes: 5 additions & 0 deletions src/rate_limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ pub(crate) struct TokenLimiter<C: Clock> {
last_update: Instant,
}

pub(crate) struct RateLimiterCfg {
pub(crate) call_count: u8,
pub(crate) call_period_secs: Duration,
}

impl<C: Clock> TokenLimiter<C> {
/// new creates a TokenLimiter initialized with the clock's current time and loads the set of
/// peers provided with each allocated call_count hits for the current period.
Expand Down
2 changes: 2 additions & 0 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ pub async fn setup_lndk(
lnd: lnd_cfg,
signals,
skip_version_check: false,
rate_limit_count: 10,
rate_limit_period_secs: 1,
};

// Make sure lndk successfully sends the invoice_request.
Expand Down
4 changes: 4 additions & 0 deletions tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ async fn test_lndk_send_invoice_request() {
lnd: lnd_cfg.clone(),
signals,
skip_version_check: false,
rate_limit_count: 10,
rate_limit_period_secs: 1,
};

let mut client = lnd.client.clone().unwrap();
Expand Down Expand Up @@ -260,6 +262,8 @@ async fn test_lndk_send_invoice_request() {
lnd: lnd_cfg,
signals,
skip_version_check: false,
rate_limit_count: 10,
rate_limit_period_secs: 1,
};

let log_dir = Some(
Expand Down

0 comments on commit 8d34e5b

Please sign in to comment.