Skip to content

Commit

Permalink
add timeout for JSON RPC requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Maciej Wójcik committed Jan 23, 2025
1 parent c2f1182 commit ada2272
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/json_rpc/call.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::time::Duration;

use crate::{Error, JsonRpcId, SuccessResponse, Verbosity};
use jsonrpc_lite::{JsonRpc, Params};
use once_cell::sync::OnceCell;
Expand All @@ -6,6 +8,8 @@ use serde::{de::DeserializeOwned, Serialize};
use serde_json::json;

const RPC_API_PATH: &str = "rpc";
const REQUEST_TIMEOUT: Duration = Duration::from_secs(5);

/// Statically declared client used when making HTTP requests
/// so opened connections are pooled.
static CLIENT: OnceCell<Client> = OnceCell::new();
Expand Down Expand Up @@ -62,7 +66,12 @@ impl Call {
#[cfg(feature = "std-fs-io")]
crate::json_pretty_print(&rpc_request, self.verbosity)?;

let client = CLIENT.get_or_init(Client::new);
let client = CLIENT.get_or_init(|| {
Client::builder()
.timeout(REQUEST_TIMEOUT)

Check failure on line 71 in lib/json_rpc/call.rs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-20.04)

no method named `timeout` found for struct `ClientBuilder` in the current scope
.build()
.expect("failed to initialize HTTP client")
});
let http_response = client
.post(self.node_address)
.json(&rpc_request)
Expand Down

0 comments on commit ada2272

Please sign in to comment.