Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Grpcd: Add helper function for executing health check #883

Merged
merged 1 commit into from
Dec 27, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 33 additions & 103 deletions src/grpcd/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use crate::bus::Outcome;
use crate::grpcd::runtime::farcaster::NetworkSelector;
use crate::service::Endpoints;
use crate::swapd::StateReport;
use crate::syncerd::Health;
use crate::syncerd::SweepAddressAddendum;
use crate::syncerd::SweepBitcoinAddress;
use crate::syncerd::SweepMoneroAddress;
Expand Down Expand Up @@ -320,6 +321,24 @@ impl FarcasterService {
drop(pending_requests);
Ok(oneshot_rx)
}

async fn check_health(
&self,
blockchain: Blockchain,
network: Network,
) -> Result<Health, Status> {
let oneshot_rx = self
.process_request(BusMsg::Bridge(BridgeMsg::Ctl {
request: CtlMsg::HealthCheck(blockchain, network),
service_id: ServiceId::Farcasterd,
}))
.await?;

match oneshot_rx.await {
Ok(BusMsg::Ctl(CtlMsg::HealthResult(health))) => Ok(health),
_ => Err(Status::internal("Error during health check".to_string())),
}
}
}

fn process_error_response<T>(msg: Result<BusMsg, RecvError>) -> Result<GrpcResponse<T>, Status> {
Expand Down Expand Up @@ -1028,33 +1047,9 @@ impl Farcaster for FarcasterService {

match selector {
HealthCheckSelector::Network(network) => {
let oneshot_rx = self
.process_request(BusMsg::Bridge(BridgeMsg::Ctl {
request: CtlMsg::HealthCheck(Blockchain::Bitcoin, network),
service_id: ServiceId::Farcasterd,
}))
.await?;
let bitcoin_health = self.check_health(Blockchain::Bitcoin, network).await?;
let monero_health = self.check_health(Blockchain::Monero, network).await?;

let bitcoin_health = match oneshot_rx.await {
Ok(BusMsg::Ctl(CtlMsg::HealthResult(health))) => health,
_ => {
return Err(Status::internal("Error during health check".to_string()));
}
};

let oneshot_rx = self
.process_request(BusMsg::Bridge(BridgeMsg::Ctl {
request: CtlMsg::HealthCheck(Blockchain::Monero, network),
service_id: ServiceId::Farcasterd,
}))
.await?;

let monero_health = match oneshot_rx.await {
Ok(BusMsg::Ctl(CtlMsg::HealthResult(health))) => health,
_ => {
return Err(Status::internal("Error during health check".to_string()));
}
};
Ok(GrpcResponse::new(HealthCheckResponse {
id,
health_report: Some(
Expand All @@ -1068,90 +1063,25 @@ impl Farcaster for FarcasterService {
}))
}
HealthCheckSelector::All => {
let oneshot_rx = self
.process_request(BusMsg::Bridge(BridgeMsg::Ctl {
request: CtlMsg::HealthCheck(Blockchain::Bitcoin, Network::Testnet),
service_id: ServiceId::Farcasterd,
}))
let bitcoin_testnet_health = self
.check_health(Blockchain::Bitcoin, Network::Testnet)
.await?;

let bitcoin_testnet_health = match oneshot_rx.await {
Ok(BusMsg::Ctl(CtlMsg::HealthResult(health))) => health,
_ => {
return Err(Status::internal("Error during health check".to_string()));
}
};

let oneshot_rx = self
.process_request(BusMsg::Bridge(BridgeMsg::Ctl {
request: CtlMsg::HealthCheck(Blockchain::Bitcoin, Network::Mainnet),
service_id: ServiceId::Farcasterd,
}))
let bitcoin_mainnet_health = self
.check_health(Blockchain::Bitcoin, Network::Mainnet)
.await?;

let bitcoin_mainnet_health = match oneshot_rx.await {
Ok(BusMsg::Ctl(CtlMsg::HealthResult(health))) => health,
_ => {
return Err(Status::internal("Error during health check".to_string()));
}
};

let oneshot_rx = self
.process_request(BusMsg::Bridge(BridgeMsg::Ctl {
request: CtlMsg::HealthCheck(Blockchain::Bitcoin, Network::Local),
service_id: ServiceId::Farcasterd,
}))
let bitcoin_local_health = self
.check_health(Blockchain::Bitcoin, Network::Local)
.await?;

let bitcoin_local_health = match oneshot_rx.await {
Ok(BusMsg::Ctl(CtlMsg::HealthResult(health))) => health,
_ => {
return Err(Status::internal("Error during health check".to_string()));
}
};

let oneshot_rx = self
.process_request(BusMsg::Bridge(BridgeMsg::Ctl {
request: CtlMsg::HealthCheck(Blockchain::Monero, Network::Testnet),
service_id: ServiceId::Farcasterd,
}))
let monero_testnet_health = self
.check_health(Blockchain::Monero, Network::Testnet)
.await?;

let monero_testnet_health = match oneshot_rx.await {
Ok(BusMsg::Ctl(CtlMsg::HealthResult(health))) => health,
_ => {
return Err(Status::internal("Error during health check".to_string()));
}
};

let oneshot_rx = self
.process_request(BusMsg::Bridge(BridgeMsg::Ctl {
request: CtlMsg::HealthCheck(Blockchain::Monero, Network::Mainnet),
service_id: ServiceId::Farcasterd,
}))
let monero_mainnet_health = self
.check_health(Blockchain::Monero, Network::Mainnet)
.await?;

let monero_mainnet_health = match oneshot_rx.await {
Ok(BusMsg::Ctl(CtlMsg::HealthResult(health))) => health,
_ => {
return Err(Status::internal("Error during health check".to_string()));
}
};

let oneshot_rx = self
.process_request(BusMsg::Bridge(BridgeMsg::Ctl {
request: CtlMsg::HealthCheck(Blockchain::Monero, Network::Local),
service_id: ServiceId::Farcasterd,
}))
let monero_local_health = self
.check_health(Blockchain::Monero, Network::Local)
.await?;

let monero_local_health = match oneshot_rx.await {
Ok(BusMsg::Ctl(CtlMsg::HealthResult(health))) => health,
_ => {
return Err(Status::internal("Error during health check".to_string()));
}
};

Ok(GrpcResponse::new(HealthCheckResponse {
id,
health_report: Some(
Expand Down