From d5fd4de790a11faa835813e660a6770edf83a464 Mon Sep 17 00:00:00 2001 From: Ashok Menon Date: Wed, 18 Dec 2024 14:50:55 +0000 Subject: [PATCH] rpc-alt: configure max connections ## Description Make it possible to configure the number of max accepted connections over the command-line args. After this change, the RPC now accepts two connection count parameters (one for the DB connection pool, and one for its own accepted connections) so we also tweak the DB connection pool argument name to make the difference clearer. This change also introduces defaults for the RPC's args, for convenience. ## Test plan By default: ``` sui$ cargo run -p sui-indexer-alt-jsonrpc -- \ --database-url $DB 2024-12-18T14:50:26.798833Z INFO sui_indexer_alt_jsonrpc: Starting JSON-RPC service on 0.0.0.0:6000 2024-12-18T14:50:30.565621Z INFO connection{remote_addr=127.0.0.1:49379 conn_id=0}: jsonrpsee_server::server: Accepting new connection 1/100 ``` Overridden: ``` sui$ cargo run -p sui-indexer-alt-jsonrpc -- \ --database-url $DB --max-rpc-connections 200 2024-12-18T14:50:26.798833Z INFO sui_indexer_alt_jsonrpc: Starting JSON-RPC service on 0.0.0.0:6000 2024-12-18T14:50:30.565621Z INFO connection{remote_addr=127.0.0.1:49379 conn_id=0}: jsonrpsee_server::server: Accepting new connection 1/200 ``` --- crates/sui-indexer-alt-jsonrpc/src/lib.rs | 31 ++++++++++++++++++----- crates/sui-pg-db/src/lib.rs | 8 +++--- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/crates/sui-indexer-alt-jsonrpc/src/lib.rs b/crates/sui-indexer-alt-jsonrpc/src/lib.rs index f6d86429d00de..1b1a6c9358ed8 100644 --- a/crates/sui-indexer-alt-jsonrpc/src/lib.rs +++ b/crates/sui-indexer-alt-jsonrpc/src/lib.rs @@ -18,8 +18,12 @@ pub mod args; #[derive(clap::Args, Debug, Clone)] pub struct RpcArgs { /// Address to listen to for incoming JSON-RPC connections. - #[clap(long)] + #[clap(long, default_value_t = Self::default().listen_address)] listen_address: SocketAddr, + + /// The maximum number of concurrent connections to accept. + #[clap(long, default_value_t = Self::default().max_rpc_connections)] + max_rpc_connections: u32, } pub struct RpcService { @@ -35,10 +39,14 @@ impl RpcService { /// bind to its socket upon construction (and will fail if this is not possible), but will not /// accept connections until [Self::run] is called. pub async fn new(rpc_args: RpcArgs) -> anyhow::Result { - let RpcArgs { listen_address } = rpc_args; + let RpcArgs { + listen_address, + max_rpc_connections, + } = rpc_args; let server = ServerBuilder::new() .http_only() + .max_connections(max_rpc_connections) .build(listen_address) .await .context("Failed to bind server")?; @@ -78,6 +86,15 @@ impl RpcService { } } +impl Default for RpcArgs { + fn default() -> Self { + Self { + listen_address: "0.0.0.0:6000".parse().unwrap(), + max_rpc_connections: 100, + } + } +} + pub async fn start_rpc(args: Args) -> anyhow::Result<()> { let Args { db_args, rpc_args } = args; @@ -114,10 +131,12 @@ mod tests { } async fn test_service() -> RpcService { - let listen_address = test_listen_address(); - RpcService::new(RpcArgs { listen_address }) - .await - .expect("Failed to create test JSON-RPC service") + RpcService::new(RpcArgs { + listen_address: test_listen_address(), + ..Default::default() + }) + .await + .expect("Failed to create test JSON-RPC service") } #[tokio::test] diff --git a/crates/sui-pg-db/src/lib.rs b/crates/sui-pg-db/src/lib.rs index ca9b4178be299..d3062848bcef4 100644 --- a/crates/sui-pg-db/src/lib.rs +++ b/crates/sui-pg-db/src/lib.rs @@ -25,8 +25,8 @@ pub struct DbArgs { database_url: Url, /// Number of connections to keep in the pool. - #[arg(long, default_value_t = Self::default().connection_pool_size)] - connection_pool_size: u32, + #[arg(long, default_value_t = Self::default().db_connection_pool_size)] + db_connection_pool_size: u32, /// Time spent waiting for a connection from the pool to become available, in milliseconds. #[arg(long, default_value_t = Self::default().connection_timeout_ms)] @@ -169,7 +169,7 @@ impl Default for DbArgs { "postgres://postgres:postgrespw@localhost:5432/sui_indexer_alt", ) .unwrap(), - connection_pool_size: 100, + db_connection_pool_size: 100, connection_timeout_ms: 60_000, } } @@ -193,7 +193,7 @@ async fn pool(args: DbArgs) -> anyhow::Result> { let manager = AsyncDieselConnectionManager::new(args.database_url.as_str()); Ok(Pool::builder() - .max_size(args.connection_pool_size) + .max_size(args.db_connection_pool_size) .connection_timeout(args.connection_timeout()) .build(manager) .await?)