Skip to content

Commit

Permalink
update health response check
Browse files Browse the repository at this point in the history
  • Loading branch information
ermalkaleci committed Apr 2, 2024
1 parent 8e6303a commit 55c40b1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 41 deletions.
58 changes: 25 additions & 33 deletions src/extensions/client/health.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,29 +95,6 @@ impl Health {
None => return,
};

// Check if the endpoint has the health method
match client
.request::<serde_json::Value, Vec<serde_json::Value>>("rpc_methods", vec![])
.await
{
Ok(response) => {
let has_health_method = response
.get("methods")
.unwrap_or(&serde_json::json!([]))
.as_array()
.map(|methods| methods.iter().any(|x| x.as_str() == Some(method_name)))
.unwrap_or_default();
if !has_health_method {
tracing::warn!(
"Endpoint {url} does not have the {method_name:?} method",
url = health.url
);
return;
}
}
Err(_) => return,
};

loop {
// Wait for the next interval
tokio::time::sleep(interval).await;
Expand All @@ -130,16 +107,31 @@ impl Health {
Ok(response) => {
let duration = request_start.elapsed();

// Check if the node is syncing
if response
.get("isSyncing")
.unwrap_or(&serde_json::json!(false))
.as_bool()
.unwrap_or_default()
{
health.update(Event::StaleChain);
continue;
}
// Check known health responses
match method_name {
"system_health" => {
// Substrate node
if let Some(true) = response.get("isSyncing").and_then(|x| x.as_bool()) {
health.update(Event::StaleChain);
continue;
}
}
"net_health" => {
// Eth-RPC-Adapter (bodhijs)
if let Some(false) = response.get("isHealthy").and_then(|x| x.as_bool()) {
health.update(Event::StaleChain);
continue;
}
}
"eth_syncing" => {
// Ethereum node
if let Some(true) = response.as_bool() {
health.update(Event::StaleChain);
continue;
}
}
_ => {}
};

// Check response time
if duration > healthy_response_time {
Expand Down
8 changes: 0 additions & 8 deletions src/extensions/client/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,9 @@ async fn retry_requests_out_of_retries() {
async fn health_check_works() {
let (addr1, handle1) = dummy_server_extend(Box::new(|builder| {
let mut system_health = builder.register_method("system_health");
let mut rpc_methods = builder.register_method("rpc_methods");
tokio::spawn(async move {
loop {
tokio::select! {
Some(req) = rpc_methods.recv() => {
req.respond(json!({ "methods": ["system_health"] }));
}
Some(req) = system_health.recv() => {
req.respond(json!({ "isSyncing": true, "peers": 1, "shouldHavePeers": true }));
}
Expand All @@ -241,13 +237,9 @@ async fn health_check_works() {

let (addr2, handle2) = dummy_server_extend(Box::new(|builder| {
let mut system_health = builder.register_method("system_health");
let mut rpc_methods = builder.register_method("rpc_methods");
tokio::spawn(async move {
loop {
tokio::select! {
Some(req) = rpc_methods.recv() => {
req.respond(json!({ "methods": ["system_health"] }));
}
Some(req) = system_health.recv() => {
req.respond(json!({ "isSyncing": false, "peers": 1, "shouldHavePeers": true }));
}
Expand Down

0 comments on commit 55c40b1

Please sign in to comment.