Skip to content

Commit 33fbb57

Browse files
committed
Add Tokio example to process_events_async docs
1 parent bc54441 commit 33fbb57

File tree

1 file changed

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

1 file changed

+44
-1
lines changed

lightning-background-processor/src/lib.rs

+44-1
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ use core::task;
466466
///
467467
/// `sleeper` should return a future which completes in the given amount of time and returns a
468468
/// boolean indicating whether the background processing should exit. Once `sleeper` returns a
469-
/// future which outputs true, the loop will exit and this function's future will complete.
469+
/// future which outputs `true`, the loop will exit and this function's future will complete.
470470
///
471471
/// See [`BackgroundProcessor::start`] for information on which actions this handles.
472472
///
@@ -479,6 +479,49 @@ use core::task;
479479
/// mobile device, where we may need to check for interruption of the application regularly. If you
480480
/// are unsure, you should set the flag, as the performance impact of it is minimal unless there
481481
/// are hundreds or thousands of simultaneous process calls running.
482+
///
483+
/// For example, in order to process background events in a [Tokio](https://tokio.rs/) task, you
484+
/// could setup `process_events_async` like this:
485+
/// ```ignore
486+
/// let background_persister = Arc::clone(&my_persister);
487+
/// let background_event_handler = Arc::clone(&my_event_handler);
488+
/// let background_chain_mon = Arc::clone(&my_chain_monitor);
489+
/// let background_chan_man = Arc::clone(&my_channel_manager);
490+
/// let background_gossip_sync = GossipSync::p2p(Arc::clone(&my_gossip_sync));
491+
/// let background_peer_man = Arc::clone(&my_peer_manager);
492+
/// let background_logger = Arc::clone(&my_logger);
493+
/// let background_scorer = Arc::clone(&my_scorer);
494+
///
495+
/// // Setup the sleeper.
496+
/// let should_stop = AtomicBool::new(false);
497+
///
498+
/// let sleeper = |d| async move {
499+
/// tokio::time::sleep(d).await;
500+
/// should_stop.load(Ordering::Relaxed)
501+
/// };
502+
///
503+
/// let mobile_interruptable_platform = false;
504+
///
505+
/// tokio::spawn(async move {
506+
/// process_events_async(
507+
/// background_persister,
508+
/// |e| background_event_handler.handle_event(e),
509+
/// background_chain_mon,
510+
/// background_chan_man,
511+
/// background_gossip_sync,
512+
/// background_peer_man,
513+
/// background_logger,
514+
/// Some(background_scorer),
515+
/// sleeper,
516+
/// mobile_interruptable_platform,
517+
/// )
518+
/// .await
519+
/// .expect("Failed to process events");
520+
/// });
521+
///
522+
/// // Stop the background processing.
523+
/// should_stop.store(true, Ordering::Relaxed);
524+
///```
482525
#[cfg(feature = "futures")]
483526
pub async fn process_events_async<
484527
'a,

0 commit comments

Comments
 (0)