Skip to content

Commit efdc869

Browse files
committed
Add Tokio example to process_events_async docs
1 parent 3bd395f commit efdc869

File tree

1 file changed

+41
-1
lines changed
  • lightning-background-processor/src

1 file changed

+41
-1
lines changed

lightning-background-processor/src/lib.rs

+41-1
Original file line numberDiff line numberDiff line change
@@ -350,14 +350,54 @@ macro_rules! define_run_body {
350350
///
351351
/// `sleeper` should return a future which completes in the given amount of time and returns a
352352
/// boolean indicating whether the background processing should exit. Once `sleeper` returns a
353-
/// future which outputs true, the loop will exit and this function's future will complete.
353+
/// future which outputs `true`, the loop will exit and this function's future will complete.
354354
///
355355
/// See [`BackgroundProcessor::start`] for information on which actions this handles.
356356
///
357357
/// Requires the `futures` feature. Note that while this method is available without the `std`
358358
/// feature, doing so will skip calling [`NetworkGraph::remove_stale_channels_and_tracking`],
359359
/// you should call [`NetworkGraph::remove_stale_channels_and_tracking_with_time`] regularly
360360
/// manually instead.
361+
///
362+
/// For example, in order to process background events in a [Tokio](https://tokio.rs/) task, you
363+
/// could setup `process_events_async` like this:
364+
/// ```ignore
365+
/// let background_persister = Arc::clone(&my_persister);
366+
/// let background_event_handler = Arc::clone(&my_event_handler);
367+
/// let background_chain_mon = Arc::clone(&my_chain_monitor);
368+
/// let background_chan_man = Arc::clone(&mu_channel_manager);
369+
/// let background_gossip_sync = GossipSync::p2p(Arc::clone(&my_gossip_sync));
370+
/// let background_peer_man = Arc::clone(&my_peer_manager);
371+
/// let background_logger = Arc::clone(&my_logger);
372+
/// let background_scorer = Arc::clone(&my_scorer);
373+
///
374+
/// // Setup the sleeper.
375+
/// let should_stop = AtomicBool::new(false);
376+
///
377+
/// let sleeper = |d| async move {
378+
/// tokio::time::sleep(d).await;
379+
/// should_stop.load(Ordering::Relaxed)
380+
/// };
381+
///
382+
/// tokio::spawn(async move {
383+
/// process_events_async(
384+
/// background_persister,
385+
/// |e| background_event_handler.handle_event(e),
386+
/// background_chain_mon,
387+
/// background_chan_man,
388+
/// background_gossip_sync,
389+
/// background_peer_man,
390+
/// background_logger,
391+
/// Some(background_scorer),
392+
/// sleeper,
393+
/// )
394+
/// .await
395+
/// .expect("Failed to process events");
396+
/// });
397+
///
398+
/// // Stop the background processing.
399+
/// should_stop.store(true, Ordering::Relaxed);
400+
///```
361401
#[cfg(feature = "futures")]
362402
pub async fn process_events_async<
363403
'a,

0 commit comments

Comments
 (0)