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