Skip to content

Commit

Permalink
rpc-alt: configure max connections
Browse files Browse the repository at this point in the history
## 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
```
  • Loading branch information
amnn committed Dec 19, 2024
1 parent 034bfa7 commit d5fd4de
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
31 changes: 25 additions & 6 deletions crates/sui-indexer-alt-jsonrpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<Self> {
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")?;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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]
Expand Down
8 changes: 4 additions & 4 deletions crates/sui-pg-db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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,
}
}
Expand All @@ -193,7 +193,7 @@ async fn pool(args: DbArgs) -> anyhow::Result<Pool<AsyncPgConnection>> {
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?)
Expand Down

0 comments on commit d5fd4de

Please sign in to comment.