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
+3
View File
@@ -39,6 +39,7 @@ use sp_core::crypto::SecretString;
use std::{
io, iter,
net::SocketAddr,
num::NonZeroU32,
path::{Path, PathBuf},
};
use tempfile::TempDir;
@@ -102,6 +103,8 @@ pub struct Configuration {
pub rpc_port: u16,
/// The number of messages the JSON-RPC server is allowed to keep in memory.
pub rpc_message_buffer_capacity: u32,
/// RPC rate limit per minute.
pub rpc_rate_limit: Option<NonZeroU32>,
/// Prometheus endpoint configuration. `None` if disabled.
pub prometheus_config: Option<PrometheusConfig>,
/// Telemetry service URL. `None` if disabled.
+1
View File
@@ -403,6 +403,7 @@ where
id_provider: rpc_id_provider,
cors: config.rpc_cors.as_ref(),
tokio_handle: config.tokio_handle.clone(),
rate_limit: config.rpc_rate_limit,
};
// TODO: https://github.com/paritytech/substrate/issues/13773