Skip to content

Commit

Permalink
fix: add timeout to checkpoint requests (ethereum#1137)
Browse files Browse the repository at this point in the history
  • Loading branch information
morph-dev authored Feb 2, 2024
1 parent c4a405c commit 9a633ee
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions light-client/src/config/checkpoints.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::collections::HashMap;
use std::{collections::HashMap, time::Duration};

use anyhow::anyhow;
use ethereum_types::H256;
use reqwest::Response;
use serde::{Deserialize, Serialize};
use serde_this_or_that::as_u64;

Expand All @@ -10,6 +11,8 @@ use crate::config::networks;
/// The location where the list of checkpoint services are stored.
pub const CHECKPOINT_SYNC_SERVICES_LIST: &str = "https://raw.githubusercontent.com/ethpandaops/checkpoint-sync-health-checks/master/_data/endpoints.yaml";

const REQUEST_TIMEOUT_SEC: u64 = 5;

#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RawSlotResponse {
pub data: RawSlotResponseData,
Expand Down Expand Up @@ -91,8 +94,7 @@ impl CheckpointFallback {
/// The list is defined in [ethPandaOps/checkpoint-fallback-service](https://github.com/ethpandaops/checkpoint-sync-health-checks/blob/master/_data/endpoints.yaml).
pub async fn build(mut self) -> anyhow::Result<Self> {
// Fetch the services
let client = reqwest::Client::new();
let res = client.get(CHECKPOINT_SYNC_SERVICES_LIST).send().await?;
let res = Self::send_request(CHECKPOINT_SYNC_SERVICES_LIST).await?;
let yaml = res.text().await?;

// Parse the yaml content results.
Expand Down Expand Up @@ -126,9 +128,8 @@ impl CheckpointFallback {
}

async fn query_service(endpoint: &str) -> Option<RawSlotResponse> {
let client = reqwest::Client::new();
let constructed_url = Self::construct_url(endpoint);
let res = client.get(&constructed_url).send().await.ok()?;
let res = Self::send_request(&constructed_url).await.ok()?;
let raw = res.json().await.ok()?;
Some(raw)
}
Expand Down Expand Up @@ -205,9 +206,8 @@ impl CheckpointFallback {
/// service api url.
pub async fn fetch_checkpoint_from_api(url: &str) -> anyhow::Result<H256> {
// Fetch the url
let client = reqwest::Client::new();
let constructed_url = Self::construct_url(url);
let res = client.get(constructed_url).send().await?;
let res = Self::send_request(&constructed_url).await?;
let raw: RawSlotResponse = res.json().await?;
let slot = raw.data.slots[0].clone();
slot.block_root
Expand Down Expand Up @@ -286,6 +286,15 @@ impl CheckpointFallback {
) -> &Vec<CheckpointFallbackService> {
self.services[network].as_ref()
}

async fn send_request(url: &str) -> anyhow::Result<Response> {
let client = reqwest::Client::new();
Ok(client
.get(url)
.timeout(Duration::from_secs(REQUEST_TIMEOUT_SEC))
.send()
.await?)
}
}

#[cfg(test)]
Expand Down

0 comments on commit 9a633ee

Please sign in to comment.