Skip to content

Commit

Permalink
added gRPC server health check
Browse files Browse the repository at this point in the history
  • Loading branch information
idky137 committed Jun 7, 2024
1 parent d56984e commit bd0c4c0
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
50 changes: 49 additions & 1 deletion zingo-proxyd/src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//! - Update spawn_server and nym_spawn to return <Result<(), GrpcServerError>> and <Result<(), NymServerError>> and use here.

use crate::{nym_server::NymServer, server::spawn_server};
use zcash_client_backend::proto::service::compact_tx_streamer_client::CompactTxStreamerClient;
use zingo_rpc::primitives::NymClient;

use std::sync::atomic::{AtomicBool, Ordering};
Expand All @@ -28,7 +29,7 @@ pub async fn spawn_proxy(
println!("@zingoproxyd: Launching Zingo-Proxy..\n@zingoproxyd: Launching gRPC Server..");
let proxy_handle = spawn_server(proxy_port, lwd_port, zebrad_port, online.clone()).await;
handles.push(proxy_handle);
tokio::time::sleep(tokio::time::Duration::from_secs(2)).await;
wait_on_grpc_startup(proxy_port, online.clone()).await;

#[cfg(not(feature = "nym_poc"))]
{
Expand All @@ -38,6 +39,7 @@ pub async fn spawn_proxy(

let nym_proxy_handle = nym_server.serve(online).await;
handles.push(nym_proxy_handle);
// TODO: Add wait_on_nym_startup(nym_addr_out, online.clone()) function to test nym server.
tokio::time::sleep(tokio::time::Duration::from_secs(2)).await;
}

Expand All @@ -53,6 +55,52 @@ pub async fn close_proxy(online: Arc<AtomicBool>) {
online.store(false, Ordering::SeqCst);
}

/// Tries to connect to the gRPC server and retruns if connection established. Shuts down with error message if connection with server cannot be established after 3 attempts.
async fn wait_on_grpc_startup(proxy_port: &u16, online: Arc<AtomicBool>) {
let proxy_uri = http::Uri::builder()
.scheme("http")
.authority(format!("localhost:{proxy_port}"))
.path_and_query("/")
.build()
.unwrap();
let mut attempts = 0;
let mut interval = tokio::time::interval(tokio::time::Duration::from_millis(500));
interval.tick().await;
while attempts < 3 {
match CompactTxStreamerClient::connect(proxy_uri.clone()).await {
Ok(mut client) => match client
.get_lightd_info(tonic::Request::new(
zcash_client_backend::proto::service::Empty {},
))
.await
{
Ok(_) => {
return;
}
Err(e) => {
println!(
"@zingoproxyd: GRPC server connection attempt {} failed with error: {}. Re",
attempts + 1,
e
);
}
},
Err(e) => {
println!(
"@zingoproxyd: GRPC server attempt {} failed to connect with error: {}",
attempts + 1,
e
);
}
}
attempts += 1;
interval.tick().await;
}
println!("@zingoproxyd: Failed to start gRPC server, please check system config. Exiting Zingo-Proxy...");
online.store(false, Ordering::SeqCst);
std::process::exit(1);
}

fn startup_message() {
let welcome_message = r#"
@@@@@@@@@@@@@@@&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&@@@@@@@@@
Expand Down
3 changes: 3 additions & 0 deletions zingo-rpc/src/jsonrpc/connector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ impl JsonRpcConnector {
JsonRpcConnectorError::new_with_source("Failed to read response body", Box::new(e))
})?;

// NOTE: This is useful for development but is not clear to users and should be simplified or completely removed before production.
println!(
"@zingoproxyd: Received response from {} call to node: {:#?}",
method.to_string(),
Expand Down Expand Up @@ -519,6 +520,7 @@ pub async fn test_node_and_return_uri(
let ipv6_uri: Uri = format!("http://[::1]:{}", port).parse().map_err(|e| {
JsonRpcConnectorError::new_with_source("Failed to parse IPv6 URI", Box::new(e))
})?;
let mut interval = tokio::time::interval(tokio::time::Duration::from_millis(500));

for _ in 0..3 {
println!("@zingoproxyd: Trying connection on IPv4.");
Expand Down Expand Up @@ -547,6 +549,7 @@ pub async fn test_node_and_return_uri(
}
}
}
interval.tick().await;
}

eprintln!("@zingoproxyd: Could not establish connection with node. \n@zingoproxyd: Please check config and confirm node is listening at the correct address and the correct authorisation details have been entered. \n@zingoproxyd: Exiting..");
Expand Down

0 comments on commit bd0c4c0

Please sign in to comment.