MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / spawn_loop

Function spawn_loop

nodedb/src/control/shutdown/spawn.rs:52–70  ·  view source on GitHub ↗

Spawn an async loop on the tokio runtime. The `body` closure receives a [`ShutdownReceiver`] it MUST select on to observe shutdown. Example: ```ignore use crate::control::shutdown::spawn_loop; spawn_loop( &shared.loop_registry, &shared.shutdown, "wal_catchup", |mut shutdown| async move { let mut tick = tokio::time::interval(Duration::from_millis(50)); loop { tokio::select! { _ = shutdown.wait_c

(
    registry: &LoopRegistry,
    shutdown: &ShutdownWatch,
    name: &'static str,
    body: F,
)

Source from the content-addressed store, hash-verified

50/// the caller can meaningfully handle, because a registration
51/// race means shutdown has already fired.
52pub fn spawn_loop<F, Fut>(
53 registry: &LoopRegistry,
54 shutdown: &ShutdownWatch,
55 name: &'static str,
56 body: F,
57) where
58 F: FnOnce(ShutdownReceiver) -> Fut + Send + 'static,
59 Fut: Future<Output = ()> + Send + 'static,
60{
61 let rx = shutdown.subscribe();
62 let handle = tokio::spawn(async move { body(rx).await });
63 if let Err(e) = registry.register(name, LoopHandle::Async(handle)) {
64 tracing::warn!(
65 error = %e,
66 "spawn_loop after registry close — task will run to completion \
67 but shutdown_all will not wait for it"
68 );
69 }
70}
71
72/// Spawn a blocking loop via `tokio::task::spawn_blocking`.
73/// Same semantics as [`spawn_loop`] except the body runs on a

Callers 6

spawn_idle_sweep_loopFunction · 0.85
spawn_background_loopsFunction · 0.85
spawn_response_pollerFunction · 0.85

Calls 3

spawnFunction · 0.50
subscribeMethod · 0.45
registerMethod · 0.45

Tested by

no test coverage detected