rpc server: make possible to disable/enable batch requests (#3364)

The rationale behind this, is that it may be useful for some users
actually disable RPC batch requests or limit them by length instead of
the total size bytes of the batch.

This PR adds two new CLI options:

```
--rpc-disable-batch-requests - disable batch requests on the server
--rpc-max-batch-request-len <LEN> - limit batches to LEN on the server.
```
This commit is contained in:
Niklas Adolfsson
2024-02-20 17:16:21 +01:00
committed by GitHub
parent ff40310d87
commit fee810a5ea
14 changed files with 77 additions and 11 deletions
+25 -1
View File
@@ -30,7 +30,7 @@ use crate::{
use clap::Parser;
use regex::Regex;
use sc_service::{
config::{BasePath, PrometheusConfig, TransactionPoolOptions},
config::{BasePath, PrometheusConfig, RpcBatchRequestConfig, TransactionPoolOptions},
ChainSpec, Role,
};
use sc_telemetry::TelemetryEndpoints;
@@ -125,6 +125,14 @@ pub struct RunCmd {
#[arg(long, default_value_t = RPC_DEFAULT_MESSAGE_CAPACITY_PER_CONN)]
pub rpc_message_buffer_capacity_per_connection: u32,
/// Disable RPC batch requests
#[arg(long, alias = "rpc_no_batch_requests", conflicts_with_all = &["rpc_max_batch_request_len"])]
pub rpc_disable_batch_requests: bool,
/// Limit the max length per RPC batch request
#[arg(long, conflicts_with_all = &["rpc_disable_batch_requests"], value_name = "LEN")]
pub rpc_max_batch_request_len: Option<u32>,
/// Specify browser *origins* allowed to access the HTTP & WS RPC servers.
///
/// A comma-separated list of origins (protocol://domain or special `null`
@@ -411,6 +419,22 @@ impl CliConfiguration for RunCmd {
Ok(self.rpc_max_subscriptions_per_connection)
}
fn rpc_buffer_capacity_per_connection(&self) -> Result<u32> {
Ok(self.rpc_message_buffer_capacity_per_connection)
}
fn rpc_batch_config(&self) -> Result<RpcBatchRequestConfig> {
let cfg = if self.rpc_disable_batch_requests {
RpcBatchRequestConfig::Disabled
} else if let Some(l) = self.rpc_max_batch_request_len {
RpcBatchRequestConfig::Limit(l)
} else {
RpcBatchRequestConfig::Unlimited
};
Ok(cfg)
}
fn rpc_rate_limit(&self) -> Result<Option<NonZeroU32>> {
Ok(self.rpc_rate_limit)
}
+9 -2
View File
@@ -28,7 +28,8 @@ use sc_service::{
config::{
BasePath, Configuration, DatabaseSource, KeystoreConfig, NetworkConfiguration,
NodeKeyConfig, OffchainWorkerConfig, OutputFormat, PrometheusConfig, PruningMode, Role,
RpcMethods, TelemetryEndpoints, TransactionPoolOptions, WasmExecutionMethod,
RpcBatchRequestConfig, RpcMethods, TelemetryEndpoints, TransactionPoolOptions,
WasmExecutionMethod,
},
BlocksPruning, ChainSpec, TracingReceiver,
};
@@ -338,7 +339,12 @@ pub trait CliConfiguration<DCV: DefaultConfigurationValues = ()>: Sized {
Ok(RPC_DEFAULT_MESSAGE_CAPACITY_PER_CONN)
}
/// Rate limit calls per minute.
/// RPC server batch request configuration.
fn rpc_batch_config(&self) -> Result<RpcBatchRequestConfig> {
Ok(RpcBatchRequestConfig::Unlimited)
}
/// RPC rate limit configuration.
fn rpc_rate_limit(&self) -> Result<Option<NonZeroU32>> {
Ok(None)
}
@@ -515,6 +521,7 @@ pub trait CliConfiguration<DCV: DefaultConfigurationValues = ()>: Sized {
rpc_max_subs_per_conn: self.rpc_max_subscriptions_per_connection()?,
rpc_port: DCV::rpc_listen_port(),
rpc_message_buffer_capacity: self.rpc_buffer_capacity_per_connection()?,
rpc_batch_config: self.rpc_batch_config()?,
rpc_rate_limit: self.rpc_rate_limit()?,
prometheus_config: self
.prometheus_config(DCV::prometheus_listen_port(), &chain_spec)?,
+1
View File
@@ -271,6 +271,7 @@ mod tests {
rpc_max_subs_per_conn: Default::default(),
rpc_message_buffer_capacity: Default::default(),
rpc_port: 9944,
rpc_batch_config: sc_service::config::RpcBatchRequestConfig::Unlimited,
rpc_rate_limit: None,
prometheus_config: None,
telemetry_endpoints: None,