From dfaf4efeb450d8dfee0ee01a0aab21cf2f24d648 Mon Sep 17 00:00:00 2001 From: Theo Butler Date: Wed, 8 Nov 2023 09:01:25 -0500 Subject: [PATCH] fix: share HTTP client between `DeploymentClient`s Ideally, a single `reqwest::Client` is reused so that they use the same connection pool. This also provides a clearer point to set timeout policies, etc. --- common/src/subgraph_client/client.rs | 16 ++++++++++++---- service/src/main.rs | 8 ++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/common/src/subgraph_client/client.rs b/common/src/subgraph_client/client.rs index d893a6c2e..7ae4a60d4 100644 --- a/common/src/subgraph_client/client.rs +++ b/common/src/subgraph_client/client.rs @@ -41,13 +41,15 @@ impl DeploymentDetails { } struct DeploymentClient { + pub http_client: reqwest::Client, pub status: Option>, pub query_url: Url, } impl DeploymentClient { - pub fn new(details: DeploymentDetails) -> Self { + pub fn new(http_client: reqwest::Client, details: DeploymentDetails) -> Self { Self { + http_client, status: details .deployment .zip(details.status_url) @@ -71,7 +73,8 @@ impl DeploymentClient { } } - Ok(reqwest::Client::new() + Ok(self + .http_client .post(self.query_url.as_ref()) .json(body) .header(header::USER_AGENT, "indexer-common") @@ -92,11 +95,12 @@ pub struct SubgraphClient { impl SubgraphClient { pub fn new( + http_client: reqwest::Client, local_deployment: Option, remote_deployment: DeploymentDetails, ) -> Result { - let local_client = local_deployment.map(DeploymentClient::new); - let remote_client = DeploymentClient::new(remote_deployment); + let local_client = local_deployment.map(|d| DeploymentClient::new(http_client.clone(), d)); + let remote_client = DeploymentClient::new(http_client, remote_deployment); Ok(Self { local_client, @@ -173,6 +177,7 @@ mod test { fn network_subgraph_client() -> SubgraphClient { SubgraphClient::new( + reqwest::Client::new(), None, DeploymentDetails::for_query_url(NETWORK_SUBGRAPH_URL).unwrap(), ) @@ -253,6 +258,7 @@ mod test { // Create the subgraph client let client = SubgraphClient::new( + reqwest::Client::new(), Some(DeploymentDetails::for_graph_node(&mock_server_local.uri(), deployment).unwrap()), DeploymentDetails::for_query_url(&format!( "{}/subgraphs/id/{}", @@ -325,6 +331,7 @@ mod test { // Create the subgraph client let client = SubgraphClient::new( + reqwest::Client::new(), Some(DeploymentDetails::for_graph_node(&mock_server_local.uri(), deployment).unwrap()), DeploymentDetails::for_query_url(&format!( "{}/subgraphs/id/{}", @@ -397,6 +404,7 @@ mod test { // Create the subgraph client let client = SubgraphClient::new( + reqwest::Client::new(), Some(DeploymentDetails::for_graph_node(&mock_server_local.uri(), deployment).unwrap()), DeploymentDetails::for_query_url(&format!( "{}/subgraphs/id/{}", diff --git a/service/src/main.rs b/service/src/main.rs index 31fbcd6b6..4d60e7e0b 100644 --- a/service/src/main.rs +++ b/service/src/main.rs @@ -61,6 +61,12 @@ async fn main() -> Result<(), std::io::Error> { &config.indexer_infrastructure.graph_node_query_endpoint, ); + let http_client = reqwest::Client::builder() + .tcp_nodelay(true) + .timeout(Duration::from_secs(30)) + .build() + .expect("Failed to init HTTP client"); + // Make an instance of network subgraph at either // graph_node_query_endpoint/subgraphs/id/network_subgraph_deployment // or network_subgraph_endpoint @@ -71,6 +77,7 @@ async fn main() -> Result<(), std::io::Error> { // no problem. let network_subgraph = Box::leak(Box::new( SubgraphClient::new( + http_client.clone(), config .network_subgraph .network_subgraph_deployment @@ -121,6 +128,7 @@ async fn main() -> Result<(), std::io::Error> { let escrow_subgraph = Box::leak(Box::new( SubgraphClient::new( + http_client, config .escrow_subgraph .escrow_subgraph_deployment