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 Aug 22, 2024
1 parent 259de8b commit 4ffdb95
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 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 allowed to make per rate limit period."

[[param]]
name = "rate_limit_period"
type = "u64"
default = "1"
doc = "The period in seconds over which peers are rate limited."
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ pub struct Cfg {
pub lnd: LndCfg,
pub signals: LifecycleSignals,
pub skip_version_check: bool,
pub rate_limit_count: u8,
pub rate_limit_period: u64
}

#[derive(Clone)]
Expand Down Expand Up @@ -236,6 +238,8 @@ impl LndkOnionMessenger {
onion_messenger,
network,
args.signals,
args.rate_limit_count,
args.rate_limit_period
)
.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: config.rate_limit_period,

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
12 changes: 4 additions & 8 deletions src/onion_messenger.rs
Original file line number Diff line number Diff line change
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,8 @@ impl LndkOnionMessenger {
onion_messenger: OnionMessenger<ES, NS, L, NL, MR, OMH, CMH>,
network: Network,
signals: LifecycleSignals,
rate_limit_count: u8,
rate_limit_period: u64,
) -> Result<(), ()>
where
ES::Target: EntropySource,
Expand Down Expand Up @@ -273,8 +269,8 @@ impl LndkOnionMessenger {
// events we need).
let rate_limiter = &mut TokenLimiter::new(
current_peers.keys().copied(),
DEFAULT_CALL_COUNT,
DEFAULT_CALL_FREQUENCY,
rate_limit_count,
Duration::from_secs(rate_limit_period),
TokioClock::new(),
);
let mut message_sender = CustomMessenger {
Expand Down

0 comments on commit 4ffdb95

Please sign in to comment.