Skip to content

Commit e747221

Browse files
authored
Merge pull request #262 from graphprotocol/pcv/timeouts-everywhere
fix: add a timeout to json RPC transport and subgraph requests
2 parents 3219a7e + 4f15333 commit e747221

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

crates/oracle/src/main.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ pub mod subgraph;
88
use clap::Parser;
99
use contracts::Contracts;
1010
use json_oracle_encoder::{print_encoded_json_messages, OutputKind};
11+
use reqwest::Client;
1112
use std::path::PathBuf;
13+
use std::time::Duration;
1214
use web3::transports::Http;
1315

1416
pub use config::Config;
@@ -104,7 +106,11 @@ async fn print_current_epoch(config: Config) -> anyhow::Result<()> {
104106
}
105107

106108
fn init_contracts(config: Config) -> anyhow::Result<Contracts<Http>> {
107-
let transport = Http::new(config.protocol_chain.jrpc_url.as_str())?;
109+
let client = Client::builder()
110+
.timeout(Duration::from_secs(60))
111+
.build()
112+
.unwrap();
113+
let transport = Http::with_client(client, config.protocol_chain.jrpc_url);
108114
let protocol_chain = JrpcProviderForChain::new(config.protocol_chain.id, transport);
109115
Contracts::new(
110116
protocol_chain.web3,

crates/oracle/src/runner/jrpc_utils.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use futures::{
88
FutureExt,
99
};
1010
use jsonrpc_core::{Call, Value};
11+
use reqwest::Client;
1112
use serde::{Deserialize, Serialize};
1213
use std::collections::BTreeMap;
1314
use std::ops::RangeInclusive;
@@ -43,9 +44,12 @@ impl<T> JrpcExpBackoff<T> {
4344

4445
impl JrpcExpBackoff {
4546
pub fn http(jrpc_url: Url, network: Caip2ChainId, max_wait: Duration) -> Self {
46-
// Unwrap: URLs were already parsed and are valid.
47-
let client = Http::new(jrpc_url.as_str()).expect("failed to create HTTP transport");
48-
Self::new(client, network, max_wait)
47+
let client = Client::builder()
48+
.timeout(Duration::from_secs(60))
49+
.build()
50+
.unwrap();
51+
let transport = Http::with_client(client, jrpc_url);
52+
Self::new(transport, network, max_wait)
4953
}
5054
}
5155

crates/oracle/src/subgraph.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use anyhow::ensure;
55
use graphql_client::{GraphQLQuery, Response};
66
use itertools::Itertools;
77
use reqwest::Url;
8+
use std::time::Duration;
89
use tracing::{error, info, warn};
910

1011
#[derive(Debug, thiserror::Error)]
@@ -41,6 +42,7 @@ pub async fn query_subgraph(
4142
info!("Fetching latest subgraph state");
4243

4344
let client = reqwest::Client::builder()
45+
.timeout(Duration::from_secs(60))
4446
.user_agent("block-oracle")
4547
.build()
4648
.unwrap();

0 commit comments

Comments
 (0)