|
| 1 | +use std::ops::Deref; |
| 2 | +use std::sync::{Arc, Mutex}; |
| 3 | +use std::time::Duration; |
| 4 | + |
| 5 | +use lightning::util::events::EventsProvider; |
| 6 | +use tokio::sync::broadcast; |
| 7 | +use tokio::task::JoinHandle; |
| 8 | +use tokio::time::{interval, interval_at, Instant}; |
| 9 | +use tracing::{debug, error, info, trace}; |
| 10 | + |
| 11 | +use crate::lexe::channel_manager::LexeChannelManager; |
| 12 | +use crate::lexe::peer_manager::LexePeerManager; |
| 13 | +use crate::lexe::persister::LexePersister; |
| 14 | +use crate::types::{ |
| 15 | + ChainMonitorType, InvoicePayerType, P2PGossipSyncType, |
| 16 | + ProbabilisticScorerType, |
| 17 | +}; |
| 18 | + |
| 19 | +const PROCESS_EVENTS_INTERVAL: Duration = Duration::from_millis(1000); |
| 20 | +const PEER_MANAGER_PING_INTERVAL: Duration = Duration::from_secs(15); |
| 21 | +const CHANNEL_MANAGER_TICK_INTERVAL: Duration = Duration::from_secs(60); |
| 22 | +const CHANNEL_MANAGER_POLL_INTERVAL: Duration = Duration::from_millis(1000); |
| 23 | +const NETWORK_GRAPH_INITIAL_DELAY: Duration = Duration::from_secs(60); |
| 24 | +const NETWORK_GRAPH_PRUNE_INTERVAL: Duration = Duration::from_secs(15 * 60); |
| 25 | +const PROB_SCORER_PERSIST_INTERVAL: Duration = Duration::from_secs(5 * 60); |
| 26 | + |
| 27 | +/// A Tokio-native background processor that runs on a single task and does not |
| 28 | +/// spawn any OS threads. Modeled after the lightning-background-processor crate |
| 29 | +/// provided by LDK - see that crate's implementation for more details. |
| 30 | +pub struct LexeBackgroundProcessor {} |
| 31 | + |
| 32 | +impl LexeBackgroundProcessor { |
| 33 | + #[allow(clippy::too_many_arguments)] |
| 34 | + pub fn start( |
| 35 | + channel_manager: LexeChannelManager, |
| 36 | + peer_manager: LexePeerManager, |
| 37 | + persister: LexePersister, |
| 38 | + chain_monitor: Arc<ChainMonitorType>, |
| 39 | + event_handler: Arc<InvoicePayerType>, |
| 40 | + gossip_sync: Arc<P2PGossipSyncType>, |
| 41 | + scorer: Arc<Mutex<ProbabilisticScorerType>>, |
| 42 | + mut shutdown_rx: broadcast::Receiver<()>, |
| 43 | + ) -> JoinHandle<()> { |
| 44 | + tokio::task::spawn(async move { |
| 45 | + let mut process_timer = interval(PROCESS_EVENTS_INTERVAL); |
| 46 | + let mut pm_timer = interval(PEER_MANAGER_PING_INTERVAL); |
| 47 | + let mut cm_tick_timer = interval(CHANNEL_MANAGER_TICK_INTERVAL); |
| 48 | + let mut cm_poll_timer = interval(CHANNEL_MANAGER_POLL_INTERVAL); |
| 49 | + let start = Instant::now() + NETWORK_GRAPH_INITIAL_DELAY; |
| 50 | + let mut ng_timer = interval_at(start, NETWORK_GRAPH_PRUNE_INTERVAL); |
| 51 | + let mut ps_timer = interval(PROB_SCORER_PERSIST_INTERVAL); |
| 52 | + |
| 53 | + loop { |
| 54 | + tokio::select! { |
| 55 | + // --- Event branches --- // |
| 56 | + _ = process_timer.tick() => { |
| 57 | + trace!("Processing pending events"); |
| 58 | + channel_manager |
| 59 | + .process_pending_events(&event_handler); |
| 60 | + chain_monitor |
| 61 | + .process_pending_events(&event_handler); |
| 62 | + peer_manager.process_events(); |
| 63 | + } |
| 64 | + _ = pm_timer.tick() => { |
| 65 | + debug!("Calling PeerManager::timer_tick_occurred()"); |
| 66 | + peer_manager.timer_tick_occurred(); |
| 67 | + } |
| 68 | + _ = cm_tick_timer.tick() => { |
| 69 | + debug!("Calling ChannelManager::timer_tick_occurred()"); |
| 70 | + channel_manager.timer_tick_occurred(); |
| 71 | + } |
| 72 | + |
| 73 | + // --- Persistence branches --- // |
| 74 | + _ = cm_poll_timer.tick() => { |
| 75 | + trace!("Polling channel manager for updates"); |
| 76 | + // TODO Use get_persistence_condvar_value instead |
| 77 | + let timeout = Duration::from_millis(10); |
| 78 | + let needs_persist = channel_manager |
| 79 | + .await_persistable_update_timeout(timeout); |
| 80 | + if needs_persist { |
| 81 | + // TODO Log err and shut down if persist fails |
| 82 | + persister.persist_manager(channel_manager.deref()).await |
| 83 | + .expect("TODO: Shut down if persist fails"); |
| 84 | + } |
| 85 | + } |
| 86 | + _ = ng_timer.tick() => { |
| 87 | + debug!("Pruning and persisting network graph"); |
| 88 | + let network_graph = gossip_sync.network_graph(); |
| 89 | + network_graph.remove_stale_channels(); |
| 90 | + // TODO Log err and shut down if persist fails |
| 91 | + persister.persist_graph(network_graph).await |
| 92 | + .expect("TODO: Shut down if persist fails"); |
| 93 | + } |
| 94 | + _ = ps_timer.tick() => { |
| 95 | + debug!("Persisting probabilistic scorer"); |
| 96 | + // TODO Log err and shut down if persist fails |
| 97 | + persister.persist_scorer(scorer.as_ref()).await |
| 98 | + .expect("TODO: Shut down if persist fails"); |
| 99 | + } |
| 100 | + |
| 101 | + // --- Shutdown branch --- // |
| 102 | + _ = shutdown_rx.recv() => { |
| 103 | + info!("Background processor shutting down"); |
| 104 | + break; |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + // Persist everything one last time. |
| 110 | + // - For the channel manager, this may prevent some races where the |
| 111 | + // node quits while channel updates were in-flight, causing |
| 112 | + // ChannelMonitor updates to be persisted without corresponding |
| 113 | + // ChannelManager updating being persisted. This does not risk the |
| 114 | + // loss of funds, but upon next boot the ChannelManager may |
| 115 | + // accidentally trigger a force close.. |
| 116 | + // - For the network graph and scorer, it is possible that the node |
| 117 | + // is shut down before they have gotten a chance to be persisted, |
| 118 | + // (e.g. `shutdown_after_sync_if_no_activity` is set), and since |
| 119 | + // we're already another API call for the channel manager, we |
| 120 | + // might as well concurrently persist these as well. |
| 121 | + let network_graph = gossip_sync.network_graph(); |
| 122 | + let (cm_res, ng_res, ps_res) = tokio::join!( |
| 123 | + persister.persist_manager(channel_manager.deref()), |
| 124 | + persister.persist_graph(network_graph), |
| 125 | + persister.persist_scorer(scorer.as_ref()), |
| 126 | + ); |
| 127 | + for res in [cm_res, ng_res, ps_res] { |
| 128 | + if let Err(e) = res { |
| 129 | + error!("Final persistence failure: {:#}", e); |
| 130 | + } |
| 131 | + } |
| 132 | + }) |
| 133 | + } |
| 134 | +} |
0 commit comments