Skip to content

Commit 8dc3d23

Browse files
authored
Add a default timeout to all BeaconNodeHttpClient requests (#7400)
Add a default request timeout to all `BeaconNodeHttpClient` requests to ensure that no HTTP request can hang indefinitely.
1 parent 0f13029 commit 8dc3d23

2 files changed

Lines changed: 27 additions & 16 deletions

File tree

common/eth2/src/lib.rs

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ pub struct Timeouts {
144144
pub get_debug_beacon_states: Duration,
145145
pub get_deposit_snapshot: Duration,
146146
pub get_validator_block: Duration,
147+
pub default: Duration,
147148
}
148149

149150
impl Timeouts {
@@ -161,6 +162,7 @@ impl Timeouts {
161162
get_debug_beacon_states: timeout,
162163
get_deposit_snapshot: timeout,
163164
get_validator_block: timeout,
165+
default: timeout,
164166
}
165167
}
166168
}
@@ -235,7 +237,9 @@ impl BeaconNodeHttpClient {
235237
url: U,
236238
builder: impl FnOnce(RequestBuilder) -> RequestBuilder,
237239
) -> Result<Response, Error> {
238-
let response = builder(self.client.get(url)).send().await?;
240+
let response = builder(self.client.get(url).timeout(self.timeouts.default))
241+
.send()
242+
.await?;
239243
ok_or_error(response).await
240244
}
241245

@@ -398,11 +402,10 @@ impl BeaconNodeHttpClient {
398402
body: &T,
399403
timeout: Option<Duration>,
400404
) -> Result<Response, Error> {
401-
let mut builder = self.client.post(url);
402-
if let Some(timeout) = timeout {
403-
builder = builder.timeout(timeout);
404-
}
405-
405+
let builder = self
406+
.client
407+
.post(url)
408+
.timeout(timeout.unwrap_or(self.timeouts.default));
406409
let response = builder.json(body).send().await?;
407410
ok_or_error(response).await
408411
}
@@ -415,10 +418,10 @@ impl BeaconNodeHttpClient {
415418
timeout: Option<Duration>,
416419
fork: ForkName,
417420
) -> Result<Response, Error> {
418-
let mut builder = self.client.post(url);
419-
if let Some(timeout) = timeout {
420-
builder = builder.timeout(timeout);
421-
}
421+
let builder = self
422+
.client
423+
.post(url)
424+
.timeout(timeout.unwrap_or(self.timeouts.default));
422425
let response = builder
423426
.header(CONSENSUS_VERSION_HEADER, fork.to_string())
424427
.json(body)
@@ -433,7 +436,7 @@ impl BeaconNodeHttpClient {
433436
url: U,
434437
body: &T,
435438
) -> Result<Response, Error> {
436-
let builder = self.client.post(url);
439+
let builder = self.client.post(url).timeout(self.timeouts.default);
437440
let mut headers = HeaderMap::new();
438441

439442
headers.insert(
@@ -452,10 +455,10 @@ impl BeaconNodeHttpClient {
452455
timeout: Option<Duration>,
453456
fork: ForkName,
454457
) -> Result<Response, Error> {
455-
let mut builder = self.client.post(url);
456-
if let Some(timeout) = timeout {
457-
builder = builder.timeout(timeout);
458-
}
458+
let builder = self
459+
.client
460+
.post(url)
461+
.timeout(timeout.unwrap_or(self.timeouts.default));
459462
let mut headers = HeaderMap::new();
460463
headers.insert(
461464
CONSENSUS_VERSION_HEADER,
@@ -1868,7 +1871,13 @@ impl BeaconNodeHttpClient {
18681871
.push("node")
18691872
.push("health");
18701873

1871-
let status = self.client.get(path).send().await?.status();
1874+
let status = self
1875+
.client
1876+
.get(path)
1877+
.timeout(self.timeouts.default)
1878+
.send()
1879+
.await?
1880+
.status();
18721881
if status == StatusCode::OK || status == StatusCode::PARTIAL_CONTENT {
18731882
Ok(status)
18741883
} else {

validator_client/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ const HTTP_GET_BEACON_BLOCK_SSZ_TIMEOUT_QUOTIENT: u32 = 4;
6868
const HTTP_GET_DEBUG_BEACON_STATE_QUOTIENT: u32 = 4;
6969
const HTTP_GET_DEPOSIT_SNAPSHOT_QUOTIENT: u32 = 4;
7070
const HTTP_GET_VALIDATOR_BLOCK_TIMEOUT_QUOTIENT: u32 = 4;
71+
const HTTP_DEFAULT_TIMEOUT_QUOTIENT: u32 = 4;
7172

7273
const DOPPELGANGER_SERVICE_NAME: &str = "doppelganger";
7374

@@ -307,6 +308,7 @@ impl<E: EthSpec> ProductionValidatorClient<E> {
307308
get_debug_beacon_states: slot_duration / HTTP_GET_DEBUG_BEACON_STATE_QUOTIENT,
308309
get_deposit_snapshot: slot_duration / HTTP_GET_DEPOSIT_SNAPSHOT_QUOTIENT,
309310
get_validator_block: slot_duration / HTTP_GET_VALIDATOR_BLOCK_TIMEOUT_QUOTIENT,
311+
default: slot_duration / HTTP_DEFAULT_TIMEOUT_QUOTIENT,
310312
}
311313
} else {
312314
Timeouts::set_all(slot_duration.saturating_mul(config.long_timeouts_multiplier))

0 commit comments

Comments
 (0)