use std::{
io::{self, Error, ErrorKind},
sync::Arc,
};
use avalanche_types::jsonrpc::health;
pub async fn check(url: Arc<String>, liveness: bool) -> io::Result<health::Response> {
let url_path = {
if liveness {
"ext/health/liveness"
} else {
"ext/health"
}
};
let joined = http_manager::join_uri(url.as_str(), url_path)?;
log::info!("checking for {:?}", joined);
let rb = http_manager::get_non_tls(url.as_str(), url_path).await?;
let resp: health::Response = match serde_json::from_slice(&rb) {
Ok(p) => p,
Err(e) => {
return Err(Error::new(
ErrorKind::Other,
format!("failed to decode health::Response {}", e),
));
}
};
Ok(resp)
}
pub async fn spawn_check(u: &str, liveness: bool) -> io::Result<health::Response> {
let ep_arc = Arc::new(u.to_string());
tokio::spawn(async move { check(ep_arc, liveness).await })
.await
.expect("failed spawn await")
}