@@ -466,7 +466,8 @@ use core::task;
466
466
///
467
467
/// `sleeper` should return a future which completes in the given amount of time and returns a
468
468
/// 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.
470
+ /// The `sleeper` future is free to return early after it has triggered the exit condition.
470
471
///
471
472
/// See [`BackgroundProcessor::start`] for information on which actions this handles.
472
473
///
@@ -479,6 +480,87 @@ use core::task;
479
480
/// mobile device, where we may need to check for interruption of the application regularly. If you
480
481
/// are unsure, you should set the flag, as the performance impact of it is minimal unless there
481
482
/// are hundreds or thousands of simultaneous process calls running.
483
+ ///
484
+ /// For example, in order to process background events in a [Tokio](https://tokio.rs/) task, you
485
+ /// could setup `process_events_async` like this:
486
+ /// ```
487
+ /// # struct MyPersister {}
488
+ /// # impl lightning::util::persist::KVStorePersister for MyPersister {
489
+ /// # fn persist<W: lightning::util::ser::Writeable>(&self, key: &str, object: &W) -> lightning::io::Result<()> { Ok(()) }
490
+ /// # }
491
+ /// # struct MyEventHandler {}
492
+ /// # impl MyEventHandler {
493
+ /// # async fn handle_event(&self, _: lightning::events::Event) {}
494
+ /// # }
495
+ /// # #[derive(Eq, PartialEq, Clone, Hash)]
496
+ /// # struct MySocketDescriptor {}
497
+ /// # impl lightning::ln::peer_handler::SocketDescriptor for MySocketDescriptor {
498
+ /// # fn send_data(&mut self, _data: &[u8], _resume_read: bool) -> usize { 0 }
499
+ /// # fn disconnect_socket(&mut self) {}
500
+ /// # }
501
+ /// # use std::sync::{Arc, Mutex};
502
+ /// # use std::sync::atomic::{AtomicBool, Ordering};
503
+ /// # use lightning_background_processor::{process_events_async, GossipSync};
504
+ /// # type MyBroadcaster = dyn lightning::chain::chaininterface::BroadcasterInterface + Send + Sync;
505
+ /// # type MyFeeEstimator = dyn lightning::chain::chaininterface::FeeEstimator + Send + Sync;
506
+ /// # type MyNodeSigner = dyn lightning::chain::keysinterface::NodeSigner + Send + Sync;
507
+ /// # type MyUtxoLookup = dyn lightning::routing::utxo::UtxoLookup + Send + Sync;
508
+ /// # type MyFilter = dyn lightning::chain::Filter + Send + Sync;
509
+ /// # type MyLogger = dyn lightning::util::logger::Logger + Send + Sync;
510
+ /// # type MyChainMonitor = lightning::chain::chainmonitor::ChainMonitor<lightning::chain::keysinterface::InMemorySigner, Arc<MyFilter>, Arc<MyBroadcaster>, Arc<MyFeeEstimator>, Arc<MyLogger>, Arc<MyPersister>>;
511
+ /// # type MyPeerManager = lightning::ln::peer_handler::SimpleArcPeerManager<MySocketDescriptor, MyChainMonitor, MyBroadcaster, MyFeeEstimator, MyUtxoLookup, MyLogger>;
512
+ /// # type MyNetworkGraph = lightning::routing::gossip::NetworkGraph<Arc<MyLogger>>;
513
+ /// # type MyGossipSync = lightning::routing::gossip::P2PGossipSync<Arc<MyNetworkGraph>, Arc<MyUtxoLookup>, Arc<MyLogger>>;
514
+ /// # type MyChannelManager = lightning::ln::channelmanager::SimpleArcChannelManager<MyChainMonitor, MyBroadcaster, MyFeeEstimator, MyLogger>;
515
+ /// # type MyScorer = Mutex<lightning::routing::scoring::ProbabilisticScorer<Arc<MyNetworkGraph>, Arc<MyLogger>>>;
516
+ ///
517
+ /// # async fn setup_background_processing(my_persister: Arc<MyPersister>, my_event_handler: Arc<MyEventHandler>, my_chain_monitor: Arc<MyChainMonitor>, my_channel_manager: Arc<MyChannelManager>, my_gossip_sync: Arc<MyGossipSync>, my_logger: Arc<MyLogger>, my_scorer: Arc<MyScorer>, my_peer_manager: Arc<MyPeerManager>) {
518
+ /// let background_persister = Arc::clone(&my_persister);
519
+ /// let background_event_handler = Arc::clone(&my_event_handler);
520
+ /// let background_chain_mon = Arc::clone(&my_chain_monitor);
521
+ /// let background_chan_man = Arc::clone(&my_channel_manager);
522
+ /// let background_gossip_sync = GossipSync::p2p(Arc::clone(&my_gossip_sync));
523
+ /// let background_peer_man = Arc::clone(&my_peer_manager);
524
+ /// let background_logger = Arc::clone(&my_logger);
525
+ /// let background_scorer = Arc::clone(&my_scorer);
526
+ ///
527
+ /// // Setup the sleeper.
528
+ /// let (stop_sender, stop_receiver) = tokio::sync::watch::channel(());
529
+ ///
530
+ /// let sleeper = move |d| {
531
+ /// let mut receiver = stop_receiver.clone();
532
+ /// Box::pin(async move {
533
+ /// tokio::select!{
534
+ /// _ = tokio::time::sleep(d) => false,
535
+ /// _ = receiver.changed() => true,
536
+ /// }
537
+ /// })
538
+ /// };
539
+ ///
540
+ /// let mobile_interruptable_platform = false;
541
+ ///
542
+ /// let handle = tokio::spawn(async move {
543
+ /// process_events_async(
544
+ /// background_persister,
545
+ /// |e| background_event_handler.handle_event(e),
546
+ /// background_chain_mon,
547
+ /// background_chan_man,
548
+ /// background_gossip_sync,
549
+ /// background_peer_man,
550
+ /// background_logger,
551
+ /// Some(background_scorer),
552
+ /// sleeper,
553
+ /// mobile_interruptable_platform,
554
+ /// )
555
+ /// .await
556
+ /// .expect("Failed to process events");
557
+ /// });
558
+ ///
559
+ /// // Stop the background processing.
560
+ /// stop_sender.send(()).unwrap();
561
+ /// handle.await.unwrap();
562
+ /// # }
563
+ ///```
482
564
#[ cfg( feature = "futures" ) ]
483
565
pub async fn process_events_async <
484
566
' a ,
0 commit comments