rpc server: add rate limiting middleware (#3301)

Add RPC server rate limiting which can be utilized by the CLI
`--rpc-rate-limit <calls/per minute>`

Resolves first part of
https://github.com/paritytech/polkadot-sdk/issues/3028

//cc @PierreBesson @kogeler  you might be interested in this one

---------

Co-authored-by: James Wilson <james@jsdw.me>
Co-authored-by: Xiliang Chen <xlchen1291@gmail.com>
This commit is contained in:
Niklas Adolfsson
2024-02-17 11:18:15 +01:00
committed by GitHub
parent 612587b7b6
commit de73dd9ac5
16 changed files with 257 additions and 23 deletions
+17 -1
View File
@@ -34,7 +34,10 @@ use sc_service::{
ChainSpec, Role,
};
use sc_telemetry::TelemetryEndpoints;
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use std::{
net::{IpAddr, Ipv4Addr, SocketAddr},
num::NonZeroU32,
};
/// The `run` command used to run a node.
#[derive(Debug, Clone, Parser)]
@@ -82,6 +85,15 @@ pub struct RunCmd {
)]
pub rpc_methods: RpcMethods,
/// RPC rate limiting (calls/minute) for each connection.
///
/// This is disabled by default.
///
/// For example `--rpc-rate-limit 10` will maximum allow
/// 10 calls per minute per connection.
#[arg(long)]
pub rpc_rate_limit: Option<NonZeroU32>,
/// Set the maximum RPC request payload size for both HTTP and WS in megabytes.
#[arg(long, default_value_t = RPC_DEFAULT_MAX_REQUEST_SIZE_MB)]
pub rpc_max_request_size: u32,
@@ -399,6 +411,10 @@ impl CliConfiguration for RunCmd {
Ok(self.rpc_max_subscriptions_per_connection)
}
fn rpc_rate_limit(&self) -> Result<Option<NonZeroU32>> {
Ok(self.rpc_rate_limit)
}
fn transaction_pool(&self, is_dev: bool) -> Result<TransactionPoolOptions> {
Ok(self.pool_config.transaction_pool(is_dev))
}