From e7502a723781675868ee00b1d8e53eefb35f1035 Mon Sep 17 00:00:00 2001 From: Eric Wendland Date: Sun, 5 Jul 2026 17:37:12 +0200 Subject: [PATCH] refactor: extract daemon task spawning --- crates/geth-node/src/daemon.rs | 37 ++++++++++++++++++++++++++++ crates/geth-node/src/lib.rs | 34 ++----------------------- docs/production-readiness-roadmap.md | 7 +++--- 3 files changed, 43 insertions(+), 35 deletions(-) create mode 100644 crates/geth-node/src/daemon.rs diff --git a/crates/geth-node/src/daemon.rs b/crates/geth-node/src/daemon.rs new file mode 100644 index 0000000..129add0 --- /dev/null +++ b/crates/geth-node/src/daemon.rs @@ -0,0 +1,37 @@ +//! Daemon task orchestration helpers. + +use crate::LocalNode; +use geth_iroh::GethIrohEndpoint; +use std::time::Duration; + +pub(crate) fn spawn_iroh_control_accept_loop(node: LocalNode, endpoint: GethIrohEndpoint) { + let raw_endpoint = endpoint.endpoint(); + tokio::spawn(async move { + tracing::debug!("iroh accept loop started"); + while let Some(incoming) = raw_endpoint.accept().await { + tracing::debug!("iroh incoming connection accepted by endpoint loop"); + let node = node.clone(); + tokio::spawn(async move { + if let Err(error) = super::handle_iroh_control_connection(node, incoming).await { + tracing::warn!(%error, "iroh control request failed"); + } + }); + } + tracing::debug!("iroh accept loop ended"); + }); +} + +pub(crate) fn spawn_background_live_sync(node: LocalNode, interval_duration: Duration) { + tokio::spawn(async move { + if let Err(error) = super::run_live_sync_once(&node).await { + tracing::debug!(%error, "initial live sync tick failed"); + } + let mut interval = tokio::time::interval(interval_duration); + loop { + interval.tick().await; + if let Err(error) = super::run_live_sync_once(&node).await { + tracing::debug!(%error, "live sync tick failed"); + } + } + }); +} diff --git a/crates/geth-node/src/lib.rs b/crates/geth-node/src/lib.rs index ffbd2cd..f784871 100644 --- a/crates/geth-node/src/lib.rs +++ b/crates/geth-node/src/lib.rs @@ -1,9 +1,11 @@ +mod daemon; mod runtime; pub mod service; mod sync; mod wire; use base64::Engine; +use daemon::{spawn_background_live_sync, spawn_iroh_control_accept_loop}; use futures::StreamExt; use geth_auth::{AUTH_SIGNATURE_NAMESPACE, AuthExplanation, AuthOp, AuthOpKind, AuthOpSignature}; use geth_cas::{ @@ -4334,38 +4336,6 @@ async fn request_overlay_wire( } } -fn spawn_iroh_control_accept_loop(node: LocalNode, endpoint: GethIrohEndpoint) { - let raw_endpoint = endpoint.endpoint(); - tokio::spawn(async move { - tracing::debug!("iroh accept loop started"); - while let Some(incoming) = raw_endpoint.accept().await { - tracing::debug!("iroh incoming connection accepted by endpoint loop"); - let node = node.clone(); - tokio::spawn(async move { - if let Err(error) = handle_iroh_control_connection(node, incoming).await { - tracing::warn!(%error, "iroh control request failed"); - } - }); - } - tracing::debug!("iroh accept loop ended"); - }); -} - -fn spawn_background_live_sync(node: LocalNode, interval_duration: Duration) { - tokio::spawn(async move { - if let Err(error) = run_live_sync_once(&node).await { - tracing::debug!(%error, "initial live sync tick failed"); - } - let mut interval = tokio::time::interval(interval_duration); - loop { - interval.tick().await; - if let Err(error) = run_live_sync_once(&node).await { - tracing::debug!(%error, "live sync tick failed"); - } - } - }); -} - async fn run_sync_for_peer(node: &LocalNode, peer_node: &str) -> Result { let kv_stores = Store::open(&node.paths.metadata_db())?.list_kv_stores()?; let documents = Store::open(&node.paths.metadata_db())?.list_document_resources()?; diff --git a/docs/production-readiness-roadmap.md b/docs/production-readiness-roadmap.md index b40c035..d8a7390 100644 --- a/docs/production-readiness-roadmap.md +++ b/docs/production-readiness-roadmap.md @@ -37,9 +37,10 @@ behavior. - `[~]` Extract daemon startup and runtime ownership. Acceptance criteria: - - `[ ]` Daemon startup, shutdown, signal handling, socket setup, Iroh - endpoint ownership, and background task spawning live outside the main - feature handler module. + - `[ ]` Daemon startup, shutdown, signal handling, socket setup, and Iroh + endpoint ownership live outside the main feature handler module. + - `[x]` Iroh accept-loop and background live-sync task spawning live outside + the main feature handler module. - `[x]` Runtime state is represented by narrow structs with documented ownership and locking rules. - `[ ]` Existing daemon startup and status tests pass unchanged.