Skip to content

Commit 610aa40

Browse files
committed
Add Tokio example to process_events_async docs
1 parent 2ebbe6f commit 610aa40

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
@@ -450,7 +450,7 @@ use core::task;
450450
///
451451
/// `sleeper` should return a future which completes in the given amount of time and returns a
452452
/// boolean indicating whether the background processing should exit. Once `sleeper` returns a
453-
/// future which outputs true, the loop will exit and this function's future will complete.
453+
/// future which outputs `true`, the loop will exit and this function's future will complete.
454454
///
455455
/// See [`BackgroundProcessor::start`] for information on which actions this handles.
456456
///
@@ -463,6 +463,49 @@ use core::task;
463463
/// mobile device, where we may need to check for interruption of the application regularly. If you
464464
/// are unsure, you should set the flag, as the performance impact of it is minimal unless there
465465
/// are hundreds or thousands of simultaneous process calls running.
466+
///
467+
/// For example, in order to process background events in a [Tokio](https://tokio.rs/) task, you
468+
/// could setup `process_events_async` like this:
469+
/// ```ignore
470+
/// let background_persister = Arc::clone(&my_persister);
471+
/// let background_event_handler = Arc::clone(&my_event_handler);
472+
/// let background_chain_mon = Arc::clone(&my_chain_monitor);
473+
/// let background_chan_man = Arc::clone(&my_channel_manager);
474+
/// let background_gossip_sync = GossipSync::p2p(Arc::clone(&my_gossip_sync));
475+
/// let background_peer_man = Arc::clone(&my_peer_manager);
476+
/// let background_logger = Arc::clone(&my_logger);
477+
/// let background_scorer = Arc::clone(&my_scorer);
478+
///
479+
/// // Setup the sleeper.
480+
/// let should_stop = AtomicBool::new(false);
481+
///
482+
/// let sleeper = |d| async move {
483+
/// tokio::time::sleep(d).await;
484+
/// should_stop.load(Ordering::Relaxed)
485+
/// };
486+
///
487+
/// let mobile_interruptable_platform = false;
488+
///
489+
/// tokio::spawn(async move {
490+
/// process_events_async(
491+
/// background_persister,
492+
/// |e| background_event_handler.handle_event(e),
493+
/// background_chain_mon,
494+
/// background_chan_man,
495+
/// background_gossip_sync,
496+
/// background_peer_man,
497+
/// background_logger,
498+
/// Some(background_scorer),
499+
/// sleeper,
500+
/// mobile_interruptable_platform,
501+
/// )
502+
/// .await
503+
/// .expect("Failed to process events");
504+
/// });
505+
///
506+
/// // Stop the background processing.
507+
/// should_stop.store(true, Ordering::Relaxed);
508+
///```
466509
#[cfg(feature = "futures")]
467510
pub async fn process_events_async<
468511
'a,

0 commit comments

Comments
 (0)