@@ -34,6 +34,8 @@ use std::ops::Deref;
3434/// [`ChannelManager`] persistence should be done in the background.
3535/// * Calling [`ChannelManager::timer_tick_occurred`] and [`PeerManager::timer_tick_occurred`]
3636/// at the appropriate intervals.
37+ /// * Calling [`NetworkGraph::remove_stale_channels`] (if a [`NetGraphMsgHandler`] is provided to
38+ /// [`BackgroundProcessor::start`]).
3739///
3840/// It will also call [`PeerManager::process_events`] periodically though this shouldn't be relied
3941/// upon as doing so may result in high latency.
@@ -68,6 +70,9 @@ const PING_TIMER: u64 = 30;
6870#[ cfg( test) ]
6971const PING_TIMER : u64 = 1 ;
7072
73+ /// Prune the network graph of stale entries hourly.
74+ const NETWORK_PRUNE_TIMER : u64 = 60 * 60 ;
75+
7176/// Trait which handles persisting a [`ChannelManager`] to disk.
7277///
7378/// [`ChannelManager`]: lightning::ln::channelmanager::ChannelManager
@@ -203,13 +208,16 @@ impl BackgroundProcessor {
203208 let stop_thread = Arc :: new ( AtomicBool :: new ( false ) ) ;
204209 let stop_thread_clone = stop_thread. clone ( ) ;
205210 let handle = thread:: spawn ( move || -> Result < ( ) , std:: io:: Error > {
206- let event_handler = DecoratingEventHandler { event_handler, net_graph_msg_handler } ;
211+ let event_handler = DecoratingEventHandler { event_handler, net_graph_msg_handler : net_graph_msg_handler . as_ref ( ) . map ( |t| t . deref ( ) ) } ;
207212
208213 log_trace ! ( logger, "Calling ChannelManager's timer_tick_occurred on startup" ) ;
209214 channel_manager. timer_tick_occurred ( ) ;
210215
211216 let mut last_freshness_call = Instant :: now ( ) ;
212217 let mut last_ping_call = Instant :: now ( ) ;
218+ let mut last_prune_call = Instant :: now ( ) ;
219+ let mut have_pruned = false ;
220+
213221 loop {
214222 peer_manager. process_events ( ) ;
215223 channel_manager. process_pending_events ( & event_handler) ;
@@ -247,6 +255,19 @@ impl BackgroundProcessor {
247255 peer_manager. timer_tick_occurred ( ) ;
248256 last_ping_call = Instant :: now ( ) ;
249257 }
258+
259+ // Note that we want to run a graph prune once not long after startup before
260+ // falling back to our usual hourly prunes. This avoids short-lived clients never
261+ // pruning their network graph. We run once 60 seconds after startup before
262+ // continuing our normal cadence.
263+ if last_prune_call. elapsed ( ) . as_secs ( ) > if have_pruned { NETWORK_PRUNE_TIMER } else { 60 } {
264+ if let Some ( ref handler) = net_graph_msg_handler {
265+ log_trace ! ( logger, "Pruning network graph of stale entries" ) ;
266+ handler. network_graph ( ) . remove_stale_channels ( ) ;
267+ last_prune_call = Instant :: now ( ) ;
268+ have_pruned = true ;
269+ }
270+ }
250271 }
251272 } ) ;
252273 Self { stop_thread : stop_thread_clone, thread_handle : Some ( handle) }
0 commit comments