diff --git a/Makefile b/Makefile index 0cb24b6..960ff90 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ CONTAINER?=testrpc -IMAGE?=testrpc +IMAGE?=spacecomp/testrpc:latest build: @cargo build --bin testrpc diff --git a/src/adapters/hotshot.rs b/src/adapters/hotshot.rs index f2bd1dd..79b6862 100644 --- a/src/adapters/hotshot.rs +++ b/src/adapters/hotshot.rs @@ -94,6 +94,7 @@ impl Adapter for HotshotAdapter { _iteration: u32, num_txs: usize, tx_size: usize, + timeout: Option, ) -> Result { let mut txs: Vec = Vec::new(); for _ in 0..num_txs { @@ -106,6 +107,7 @@ impl Adapter for HotshotAdapter { req_id, RPC_METHOD, serde_json::json!({ "txs": txs }), + timeout, ) .await?; diff --git a/src/adapters/mod.rs b/src/adapters/mod.rs index b38e085..203e30b 100644 --- a/src/adapters/mod.rs +++ b/src/adapters/mod.rs @@ -24,6 +24,7 @@ pub trait Adapter { iteration: u32, num_txs: usize, tx_size: usize, + timeout: Option, ) -> impl std::future::Future> + Send; } diff --git a/src/config.rs b/src/config.rs index 7d5c849..1bdf769 100644 --- a/src/config.rs +++ b/src/config.rs @@ -55,6 +55,9 @@ pub struct Config { pub rpcs: Option>, /// Rounds declaration pub rounds: Vec, + /// Timeout for each RPC request in seconds + /// If None, defaults to 15 seconds + pub timeout: Option, } #[derive(Debug, Serialize, Deserialize, Clone)] diff --git a/src/jrpc.rs b/src/jrpc.rs index 063dec8..b7ed90f 100644 --- a/src/jrpc.rs +++ b/src/jrpc.rs @@ -54,6 +54,7 @@ pub async fn send( req_id: u64, method: &str, params: Value, + timeout: Option, ) -> Result { let rpc_request = RpcRequest { jsonrpc: "2.0".to_string(), @@ -64,7 +65,10 @@ pub async fn send( if env::var("DRY_RUN").is_ok() { return send_noop(rpc_url, rpc_request).await; } - let client = reqwest::Client::new(); + let client = reqwest::ClientBuilder::new() + .timeout(timeout.unwrap_or(std::time::Duration::from_secs(15))) + .build() + .map_err(|e| TestrpcError::RpcError(format!("Failed to build client: {e}")))?; let start_time = std::time::Instant::now(); diff --git a/src/runner.rs b/src/runner.rs index c680f91..b0e10f1 100644 --- a/src/runner.rs +++ b/src/runner.rs @@ -41,9 +41,10 @@ pub async fn run( let iteration = i; let round_num = r; let adapter = cfg.adapter.clone(); + let timeout = cfg.timeout.map(|t| Duration::from_secs(t as u64)); tokio::select! { _ = task::spawn(async move { - match process_round(adapter, round, iteration, rpc_urls, round_templates).await { + match process_round(adapter, round, iteration, rpc_urls, round_templates, timeout).await { Ok(result) => { tracing::debug!("Iteration {} round {} completed", iteration, round_num); let mut results = results.write().unwrap(); @@ -91,6 +92,7 @@ async fn process_round( iteration: u32, rpc_urls: Vec, round_templates: HashMap, + timeout: Option, ) -> Result { let mut req_id = iteration as u64; let mut results = RoundResults { sent: 0, failed: 0 }; @@ -120,6 +122,7 @@ async fn process_round( iteration, template.txs, template.tx_size, + timeout, ) .await }); @@ -172,6 +175,7 @@ mod tests { 0, rpc_urls, round_templates, + Some(std::time::Duration::from_secs(5)), ) .await .unwrap();