Skip to content

Commit

Permalink
feat: allow connection fail for ping
Browse files Browse the repository at this point in the history
  • Loading branch information
alextes committed Dec 1, 2023
1 parent 1cbcd2e commit e8a8d26
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 19 deletions.
28 changes: 17 additions & 11 deletions src/geth.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use reqwest::Client;
use serde_json::{json, Value};
use tracing::debug;

use crate::env::ENV_CONFIG;

#[allow(dead_code)]
pub async fn syncing() -> anyhow::Result<bool> {
let client = reqwest::Client::new();
pub async fn syncing(geth_client: &Client) -> anyhow::Result<bool> {
let body: String =
json!({ "jsonrpc":"2.0","method":"eth_syncing","params":[],"id":1 }).to_string();
let res = client
let res = geth_client
.post(&ENV_CONFIG.geth_url)
.header("content-type", "application/json")
.body(body)
Expand All @@ -20,11 +21,10 @@ pub async fn syncing() -> anyhow::Result<bool> {
Ok(geth_sync_status)
}

pub async fn peer_count() -> anyhow::Result<u64> {
let client = reqwest::Client::new();
pub async fn peer_count(geth_client: &Client) -> anyhow::Result<u64> {
let body: String =
json!({ "jsonrpc":"2.0","method":"net_peerCount","params":[],"id":1 }).to_string();
let res = client
let res = geth_client
.post(&ENV_CONFIG.geth_url)
.header("content-type", "application/json")
.body(body)
Expand All @@ -40,15 +40,21 @@ pub async fn peer_count() -> anyhow::Result<u64> {
Ok(peer_count)
}

pub async fn ping_ok() -> anyhow::Result<bool> {
let client = reqwest::Client::new();
pub async fn ping_ok(geth_client: &Client) -> anyhow::Result<bool> {
let body: String =
json!({ "jsonrpc":"2.0","method":"net_version","params":[],"id":1 }).to_string();
let res = client
let res = geth_client
.post(&ENV_CONFIG.geth_url)
.header("content-type", "application/json")
.body(body)
.send()
.await?;
Ok(res.status().is_success())
.await;

match res {
Ok(res) => Ok(res.status().is_success()),
Err(e) => {
debug!("geth ping failed: {}", e);
Ok(false)
}
}
}
11 changes: 9 additions & 2 deletions src/lighthouse.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use reqwest::Client;
use serde::Deserialize;
use tracing::debug;

use crate::env::ENV_CONFIG;

Expand Down Expand Up @@ -105,8 +106,14 @@ pub async fn peer_counts(beacon_client: &Client) -> anyhow::Result<PeerCounts> {

pub async fn ping_ok(beacon_client: &Client) -> anyhow::Result<bool> {
let url = format!("{}/eth/v1/node/version", &ENV_CONFIG.beacon_url);
let res = beacon_client.get(url).send().await?;
Ok(res.status().is_success())
let res = beacon_client.get(url).send().await;
match res {
Ok(res) => Ok(res.status().is_success()),
Err(e) => {
debug!("lighthouse ping failed: {}", e);
Ok(false)
}
}
}

#[cfg(test)]
Expand Down
7 changes: 4 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ async fn main() -> anyhow::Result<()> {
});

let beacon_client = reqwest::Client::new();
let geth_client = reqwest::Client::new();

// It can take a long long time for the geth and lighthouse nodes to start responding to
// requests, so we wait until they are ready before we start the server.
const MAX_STARTUP_TIME: Duration = Duration::from_secs(60 * 2);
let start_time = SystemTime::now();
loop {
let geth_ping_ok = geth::ping_ok().await?;
let geth_ping_ok = geth::ping_ok(&geth_client).await?;
let lighthouse_ping_ok = node_health::lighthouse::ping_ok(&beacon_client).await?;

if geth_ping_ok && lighthouse_ping_ok {
Expand All @@ -53,7 +54,7 @@ async fn main() -> anyhow::Result<()> {
}

loop {
let geth_syncing = geth::syncing().await?;
let geth_syncing = geth::syncing(&geth_client).await?;
if geth_syncing {
info!("geth is syncing, not ready");
is_ready.store(false, std::sync::atomic::Ordering::Relaxed);
Expand All @@ -63,7 +64,7 @@ async fn main() -> anyhow::Result<()> {
debug!("geth is not syncing");
}

let geth_peer_count = geth::peer_count().await?;
let geth_peer_count = geth::peer_count(&geth_client).await?;
if geth_peer_count < 10 {
info!("geth has less than 10 peers, not ready");
is_ready.store(false, std::sync::atomic::Ordering::Relaxed);
Expand Down
9 changes: 6 additions & 3 deletions tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@ use reqwest::Client;

#[tokio::test]
async fn test_geth_peer_count() -> anyhow::Result<()> {
node_health::geth::peer_count().await?;
let geth_client = Client::new();
node_health::geth::peer_count(&geth_client).await?;
Ok(())
}

#[tokio::test]
async fn test_geth_sync_status() -> anyhow::Result<()> {
node_health::geth::syncing().await?;
let geth_client = Client::new();
node_health::geth::syncing(&geth_client).await?;
Ok(())
}

#[tokio::test]
async fn test_geth_ping_ok() -> anyhow::Result<()> {
node_health::geth::ping_ok().await?;
let geth_client = Client::new();
node_health::geth::ping_ok(&geth_client).await?;
Ok(())
}

Expand Down

0 comments on commit e8a8d26

Please sign in to comment.