Skip to content
Closed
Show file tree
Hide file tree
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
5 changes: 4 additions & 1 deletion extractors/ipc/src/ipc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use init_capnp::init::Client as InitClient;
use mining_capnp::mining::Client as MiningClient;
use proxy_capnp::thread::Client as ThreadClient;

use capnp_rpc::{RpcSystem, rpc_twoparty_capnp, twoparty};
use capnp_rpc::{Disconnector, RpcSystem, rpc_twoparty_capnp, twoparty};
use shared::{
futures::AsyncReadExt,
protobuf::ipc_extractor::BlockTip,
Expand All @@ -26,6 +26,7 @@ pub struct IpcClient {
pub mining: MiningClient,
pub thread: ThreadClient,
pub rpc_task: JoinHandle<Result<(), capnp::Error>>,
pub disconnector: Disconnector<rpc_twoparty_capnp::Side>,
}

impl IpcClient {
Expand All @@ -40,6 +41,7 @@ impl IpcClient {

let mut rpc_system = RpcSystem::new(network, None);
let init: InitClient = rpc_system.bootstrap(rpc_twoparty_capnp::Side::Server);
let disconnector = rpc_system.get_disconnector();
let rpc_task = tokio::task::spawn_local(rpc_system);

let response = init.construct_request().send().promise.await?;
Expand All @@ -58,6 +60,7 @@ impl IpcClient {
rpc_task,
thread,
mining,
disconnector,
})
}

Expand Down
40 changes: 22 additions & 18 deletions extractors/ipc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub async fn run(args: Args, mut shutdown_rx: watch::Receiver<bool>) -> Result<(
})?;
log::info!("Connected to IPC socket at {}", &args.ipc_socket_path);

let ipc_session = IpcClient::init(stream).await?;
let mut ipc_session = IpcClient::init(stream).await?;

let duration_sec = Duration::from_secs(args.query_interval);
let mut interval = time::interval(duration_sec);
Expand All @@ -79,38 +79,43 @@ pub async fn run(args: Args, mut shutdown_rx: watch::Receiver<bool>) -> Result<(
loop {
tokio::select! {
_ = interval.tick() => {
if ipc_session.rpc_task.is_finished() {
log::error!("Lost connection to IPC socket. Exiting.");
break;
if let Err(e) = fetch_and_publish_tip(&ipc_session, &nats_client).await {
log::error!("Could not fetch and publish 'BlockTip': {}", e);
}

if let Err(e) = get_tip(&ipc_session, &nats_client).await {
log::error!("Could not fetch and publish 'BlockTip': {}", e)
}
res = &mut ipc_session.rpc_task => {
match res {
Ok(Ok(())) => log::warn!("Lost IPC connection to bitcoin-node."),
Ok(Err(e)) => log::error!("Lost IPC connection to bitcoin-node: {e}"),
Err(e) => log::error!("IPC task panicked or was cancelled: {e}"),
}
break;
}
res = shutdown_rx.changed() => {
match res {
Ok(_) => {
if *shutdown_rx.borrow() {
log::info!("ipc_extractor received shutdown signal.");
ipc_session.rpc_task.abort();
break;
}
Ok(_) if *shutdown_rx.borrow() => {
log::info!("ipc_extractor received shutdown signal.");
}
Err(_) => {
_ => {
// all senders dropped -> treat as shutdown
log::warn!("The shutdown notification sender was dropped. Shutting down.");
ipc_session.rpc_task.abort();
break;
}
}
break;
}
}
}

if let Err(e) = ipc_session.disconnector.await {
log::error!("could not run disconnector during shutdown: {}", e);
}
if !ipc_session.rpc_task.is_finished() {
let _ = ipc_session.rpc_task.await;
}
Ok(())
}

async fn get_tip(
async fn fetch_and_publish_tip(
ipc_client: &IpcClient,
nats_client: &async_nats::Client,
) -> Result<(), RuntimeError> {
Expand All @@ -122,7 +127,6 @@ async fn get_tip(
let proto = Event::new(PeerObserverEvent::IpcExtractor(ipc_extractor::Ipc {
ipc_event: Some(ipc_extractor::ipc::IpcEvent::BlockTip(tip)),
}))?;

nats_client
.publish(Subject::Ipc.to_string(), proto.encode_to_vec().into())
.await?;
Expand Down
Loading