Skip to content
This repository has been archived by the owner on Dec 17, 2024. It is now read-only.

Commit

Permalink
feat: Add request_timeout to rcp_client
Browse files Browse the repository at this point in the history
  • Loading branch information
0x5459 committed May 8, 2024
1 parent b811d3a commit 4cc20ae
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 8 deletions.
14 changes: 12 additions & 2 deletions crates/cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use clap::Parser;
use clap::Subcommand;
use clap_num::number_range;
use gevulot_node::rpc_client::RpcClient;
use gevulot_node::rpc_client::RpcClientBuilder;
use gevulot_node::types::program::ResourceRequest;
use gevulot_node::types::Hash;
use gevulot_node::types::TransactionTree;
use libsecp256k1::PublicKey;
use std::net::SocketAddr;
use std::path::PathBuf;
use std::time::Duration;

#[derive(Parser, Debug)]
#[clap(author = "Gevulot Team", version, about, long_about = None)]
Expand All @@ -20,6 +21,9 @@ pub struct ArgConfiguration {
value_name = "URL"
)]
json_url: String,
/// Timeout duration for rpc request (in seconds).
#[clap(long = "rpctimeout", value_name = "RPC TIMEOUT")]
rpc_timeout: Option<u64>,
/// Private key file path to sign Tx.
#[clap(
short,
Expand Down Expand Up @@ -185,7 +189,13 @@ async fn main() {

let args = ArgConfiguration::parse();

let client = RpcClient::new(args.json_url);
let mut client_builder = RpcClientBuilder::default();
if let Some(rpc_timeout) = args.rpc_timeout {
client_builder = client_builder.request_timeout(Duration::from_secs(rpc_timeout));
}
let client = client_builder
.build(args.json_url)
.expect("build rpc client");

match args.command {
ConfCommands::GenerateKey => match gevulot_cli::keyfile::create_key_file(&args.keyfile) {
Expand Down
37 changes: 33 additions & 4 deletions crates/node/src/rpc_client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,46 @@ use jsonrpsee::{
http_client::{HttpClient, HttpClientBuilder},
};
use std::error::Error;
use std::time::Duration;

/// A RPC client builder for connecting to the Gevulot network
#[derive(Debug)]
pub struct RpcClientBuilder {
request_timeout: Duration,
}

impl Default for RpcClientBuilder {
fn default() -> Self {
Self {
request_timeout: Duration::from_secs(60),
}
}
}

impl RpcClientBuilder {
/// Set request timeout (default is 60 seconds).
pub fn request_timeout(mut self, request_timeout: Duration) -> Self {
self.request_timeout = request_timeout;
self
}

/// Returns a [RpcClient] connected to the Gevulot network running at the URI provided.
pub fn build(self, url: impl AsRef<str>) -> Result<RpcClient, Box<dyn Error>> {
let client = HttpClientBuilder::default()
.request_timeout(self.request_timeout)
.build(url)?;
Ok(RpcClient { client })
}
}

pub struct RpcClient {
client: HttpClient,
}

impl RpcClient {
#[deprecated(note = "Please use `RpcClientBuilder` instead.")]
pub fn new(url: impl AsRef<str>) -> Self {
let client = HttpClientBuilder::default()
.build(url)
.expect("http client");
RpcClient { client }
RpcClientBuilder::default().build(url).expect("http client")
}

pub async fn send_transaction(&self, tx: &Transaction<Created>) -> Result<(), Box<dyn Error>> {
Expand Down
4 changes: 2 additions & 2 deletions crates/tests/e2e-tests/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use clap::Parser;
use gevulot_node::{
rpc_client::RpcClient,
rpc_client::{RpcClient, RpcClientBuilder},
types::{
program::ResourceRequest,
transaction::{Payload, ProgramData, ProgramMetadata, Workflow, WorkflowStep},
Expand Down Expand Up @@ -39,7 +39,7 @@ pub struct ArgConfiguration {
#[tokio::main]
async fn main() -> Result<()> {
let cfg = ArgConfiguration::parse();
let client = RpcClient::new(cfg.json_rpc_url);
let client = RpcClientBuilder::default().build(cfg.json_rpc_url)?;
let file_server = Arc::new(FileServer::new(cfg.listen_addr).await);

let bs = std::fs::read(cfg.key_file)?;
Expand Down

0 comments on commit 4cc20ae

Please sign in to comment.