Skip to content

Commit

Permalink
fix: share HTTP client between DeploymentClients
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Theodus committed Nov 8, 2023
1 parent dd6c356 commit dfaf4ef
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
16 changes: 12 additions & 4 deletions common/src/subgraph_client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,15 @@ impl DeploymentDetails {
}

struct DeploymentClient {
pub http_client: reqwest::Client,
pub status: Option<Eventual<DeploymentStatus>>,
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)
Expand All @@ -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")
Expand All @@ -92,11 +95,12 @@ pub struct SubgraphClient {

impl SubgraphClient {
pub fn new(
http_client: reqwest::Client,
local_deployment: Option<DeploymentDetails>,
remote_deployment: DeploymentDetails,
) -> Result<Self, anyhow::Error> {
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,
Expand Down Expand Up @@ -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(),
)
Expand Down Expand Up @@ -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/{}",
Expand Down Expand Up @@ -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/{}",
Expand Down Expand Up @@ -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/{}",
Expand Down
8 changes: 8 additions & 0 deletions service/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit dfaf4ef

Please sign in to comment.