Skip to content

Commit b595cda

Browse files
authored
fix: share HTTP client between DeploymentClients (#87)
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. and removes an unused error case.
1 parent dd6c356 commit b595cda

File tree

5 files changed

+69
-70
lines changed

5 files changed

+69
-70
lines changed

common/src/allocations/monitor.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,15 +203,15 @@ mod test {
203203
// Set up a mock network subgraph
204204
let mock_server = MockServer::start().await;
205205
let network_subgraph = SubgraphClient::new(
206+
reqwest::Client::new(),
206207
None,
207208
DeploymentDetails::for_query_url(&format!(
208209
"{}/subgraphs/id/{}",
209210
&mock_server.uri(),
210211
*test_vectors::NETWORK_SUBGRAPH_DEPLOYMENT
211212
))
212213
.unwrap(),
213-
)
214-
.unwrap();
214+
);
215215

216216
// Mock result for current epoch requests
217217
mock_server

common/src/attestations/dispute_manager.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,15 @@ mod test {
9999
// Set up a mock network subgraph
100100
let mock_server = MockServer::start().await;
101101
let network_subgraph = SubgraphClient::new(
102+
reqwest::Client::new(),
102103
None,
103104
DeploymentDetails::for_query_url(&format!(
104105
"{}/subgraphs/id/{}",
105106
&mock_server.uri(),
106107
*test_vectors::NETWORK_SUBGRAPH_DEPLOYMENT
107108
))
108109
.unwrap(),
109-
)
110-
.unwrap();
110+
);
111111

112112
// Mock result for current epoch requests
113113
mock_server

common/src/escrow_accounts.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -127,18 +127,16 @@ mod tests {
127127
async fn test_current_accounts() {
128128
// Set up a mock escrow subgraph
129129
let mock_server = MockServer::start().await;
130-
let escrow_subgraph = Box::leak(Box::new(
131-
SubgraphClient::new(
132-
None,
133-
DeploymentDetails::for_query_url(&format!(
134-
"{}/subgraphs/id/{}",
135-
&mock_server.uri(),
136-
*test_vectors::ESCROW_SUBGRAPH_DEPLOYMENT
137-
))
138-
.unwrap(),
139-
)
130+
let escrow_subgraph = Box::leak(Box::new(SubgraphClient::new(
131+
reqwest::Client::new(),
132+
None,
133+
DeploymentDetails::for_query_url(&format!(
134+
"{}/subgraphs/id/{}",
135+
&mock_server.uri(),
136+
*test_vectors::ESCROW_SUBGRAPH_DEPLOYMENT
137+
))
140138
.unwrap(),
141-
));
139+
)));
142140

143141
let mock = Mock::given(method("POST"))
144142
.and(path(format!(

common/src/subgraph_client/client.rs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,15 @@ impl DeploymentDetails {
4141
}
4242

4343
struct DeploymentClient {
44+
pub http_client: reqwest::Client,
4445
pub status: Option<Eventual<DeploymentStatus>>,
4546
pub query_url: Url,
4647
}
4748

4849
impl DeploymentClient {
49-
pub fn new(details: DeploymentDetails) -> Self {
50+
pub fn new(http_client: reqwest::Client, details: DeploymentDetails) -> Self {
5051
Self {
52+
http_client,
5153
status: details
5254
.deployment
5355
.zip(details.status_url)
@@ -71,7 +73,8 @@ impl DeploymentClient {
7173
}
7274
}
7375

74-
Ok(reqwest::Client::new()
76+
Ok(self
77+
.http_client
7578
.post(self.query_url.as_ref())
7679
.json(body)
7780
.header(header::USER_AGENT, "indexer-common")
@@ -92,16 +95,14 @@ pub struct SubgraphClient {
9295

9396
impl SubgraphClient {
9497
pub fn new(
98+
http_client: reqwest::Client,
9599
local_deployment: Option<DeploymentDetails>,
96100
remote_deployment: DeploymentDetails,
97-
) -> Result<Self, anyhow::Error> {
98-
let local_client = local_deployment.map(DeploymentClient::new);
99-
let remote_client = DeploymentClient::new(remote_deployment);
100-
101-
Ok(Self {
102-
local_client,
103-
remote_client,
104-
})
101+
) -> Self {
102+
Self {
103+
local_client: local_deployment.map(|d| DeploymentClient::new(http_client.clone(), d)),
104+
remote_client: DeploymentClient::new(http_client, remote_deployment),
105+
}
105106
}
106107

107108
pub async fn query<T: for<'de> Deserialize<'de>>(
@@ -173,10 +174,10 @@ mod test {
173174

174175
fn network_subgraph_client() -> SubgraphClient {
175176
SubgraphClient::new(
177+
reqwest::Client::new(),
176178
None,
177179
DeploymentDetails::for_query_url(NETWORK_SUBGRAPH_URL).unwrap(),
178180
)
179-
.unwrap()
180181
}
181182

182183
#[tokio::test]
@@ -253,15 +254,15 @@ mod test {
253254

254255
// Create the subgraph client
255256
let client = SubgraphClient::new(
257+
reqwest::Client::new(),
256258
Some(DeploymentDetails::for_graph_node(&mock_server_local.uri(), deployment).unwrap()),
257259
DeploymentDetails::for_query_url(&format!(
258260
"{}/subgraphs/id/{}",
259261
mock_server_remote.uri(),
260262
deployment
261263
))
262264
.unwrap(),
263-
)
264-
.unwrap();
265+
);
265266

266267
// Query the subgraph
267268
let response: Response<Value> = client
@@ -325,15 +326,15 @@ mod test {
325326

326327
// Create the subgraph client
327328
let client = SubgraphClient::new(
329+
reqwest::Client::new(),
328330
Some(DeploymentDetails::for_graph_node(&mock_server_local.uri(), deployment).unwrap()),
329331
DeploymentDetails::for_query_url(&format!(
330332
"{}/subgraphs/id/{}",
331333
mock_server_remote.uri(),
332334
deployment
333335
))
334336
.unwrap(),
335-
)
336-
.unwrap();
337+
);
337338

338339
// Query the subgraph
339340
let response: Response<Value> = client
@@ -397,15 +398,15 @@ mod test {
397398

398399
// Create the subgraph client
399400
let client = SubgraphClient::new(
401+
reqwest::Client::new(),
400402
Some(DeploymentDetails::for_graph_node(&mock_server_local.uri(), deployment).unwrap()),
401403
DeploymentDetails::for_query_url(&format!(
402404
"{}/subgraphs/id/{}",
403405
mock_server_remote.uri(),
404406
deployment
405407
))
406408
.unwrap(),
407-
)
408-
.unwrap();
409+
);
409410

410411
// Query the subgraph
411412
let response: Response<Value> = client

service/src/main.rs

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ async fn main() -> Result<(), std::io::Error> {
6161
&config.indexer_infrastructure.graph_node_query_endpoint,
6262
);
6363

64+
let http_client = reqwest::Client::builder()
65+
.tcp_nodelay(true)
66+
.timeout(Duration::from_secs(30))
67+
.build()
68+
.expect("Failed to init HTTP client");
69+
6470
// Make an instance of network subgraph at either
6571
// graph_node_query_endpoint/subgraphs/id/network_subgraph_deployment
6672
// or network_subgraph_endpoint
@@ -69,26 +75,22 @@ async fn main() -> Result<(), std::io::Error> {
6975
// a static lifetime, which avoids having to pass around and clone `Arc`
7076
// objects everywhere. Since the network subgraph is read-only, this is
7177
// no problem.
72-
let network_subgraph = Box::leak(Box::new(
73-
SubgraphClient::new(
74-
config
75-
.network_subgraph
76-
.network_subgraph_deployment
77-
.map(|deployment| {
78-
DeploymentDetails::for_graph_node(
79-
&config.indexer_infrastructure.graph_node_query_endpoint,
80-
deployment,
81-
)
82-
})
83-
.transpose()
84-
.expect(
85-
"Failed to parse graph node query endpoint and network subgraph deployment",
86-
),
87-
DeploymentDetails::for_query_url(&config.network_subgraph.network_subgraph_endpoint)
88-
.expect("Failed to parse network subgraph endpoint"),
89-
)
90-
.expect("Failed to set up network subgraph client"),
91-
));
78+
let network_subgraph = Box::leak(Box::new(SubgraphClient::new(
79+
http_client.clone(),
80+
config
81+
.network_subgraph
82+
.network_subgraph_deployment
83+
.map(|deployment| {
84+
DeploymentDetails::for_graph_node(
85+
&config.indexer_infrastructure.graph_node_query_endpoint,
86+
deployment,
87+
)
88+
})
89+
.transpose()
90+
.expect("Failed to parse graph node query endpoint and network subgraph deployment"),
91+
DeploymentDetails::for_query_url(&config.network_subgraph.network_subgraph_endpoint)
92+
.expect("Failed to parse network subgraph endpoint"),
93+
)));
9294

9395
let indexer_allocations = indexer_allocations(
9496
network_subgraph,
@@ -119,24 +121,22 @@ async fn main() -> Result<(), std::io::Error> {
119121
// assume the models are up to date in the service.
120122
let indexer_management_db = database::connect(&config.postgres).await;
121123

122-
let escrow_subgraph = Box::leak(Box::new(
123-
SubgraphClient::new(
124-
config
125-
.escrow_subgraph
126-
.escrow_subgraph_deployment
127-
.map(|deployment| {
128-
DeploymentDetails::for_graph_node(
129-
&config.indexer_infrastructure.graph_node_query_endpoint,
130-
deployment,
131-
)
132-
})
133-
.transpose()
134-
.expect("Failed to parse graph node query endpoint and escrow subgraph deployment"),
135-
DeploymentDetails::for_query_url(&config.escrow_subgraph.escrow_subgraph_endpoint)
136-
.expect("Failed to parse escrow subgraph endpoint"),
137-
)
138-
.expect("Failed to set up escrow subgraph client"),
139-
));
124+
let escrow_subgraph = Box::leak(Box::new(SubgraphClient::new(
125+
http_client,
126+
config
127+
.escrow_subgraph
128+
.escrow_subgraph_deployment
129+
.map(|deployment| {
130+
DeploymentDetails::for_graph_node(
131+
&config.indexer_infrastructure.graph_node_query_endpoint,
132+
deployment,
133+
)
134+
})
135+
.transpose()
136+
.expect("Failed to parse graph node query endpoint and escrow subgraph deployment"),
137+
DeploymentDetails::for_query_url(&config.escrow_subgraph.escrow_subgraph_endpoint)
138+
.expect("Failed to parse escrow subgraph endpoint"),
139+
)));
140140

141141
let escrow_accounts = escrow_accounts(
142142
escrow_subgraph,

0 commit comments

Comments
 (0)