mirror of
https://github.com/lndk-org/lndk.git
synced 2026-08-13 12:33:05 +02:00
Make rate limits configureable.
Add 'rate_limit_count' and 'rate_limit_period' to configuration option.
This commit is contained in:
parent
259de8b8ef
commit
8d34e5ba4c
8 changed files with 40 additions and 9 deletions
|
|
@ -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."
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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};
|
||||
|
|
@ -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)]
|
||||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
};
|
||||
|
||||
let response_invoice_timeout = config.response_invoice_timeout;
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
@ -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,
|
||||
|
|
@ -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,
|
||||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
|
@ -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(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue