@@ -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,18 @@ 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 = if let Some ( ref handler) = net_graph_msg_handler {
212+ DecoratingEventHandler { event_handler, net_graph_msg_handler : Some ( & * * handler) }
213+ } else {
214+ DecoratingEventHandler { event_handler, net_graph_msg_handler : None }
215+ } ;
207216
208217 log_trace ! ( logger, "Calling ChannelManager's timer_tick_occurred on startup" ) ;
209218 channel_manager. timer_tick_occurred ( ) ;
210219
211220 let mut last_freshness_call = Instant :: now ( ) ;
212221 let mut last_ping_call = Instant :: now ( ) ;
222+ let mut last_prune_call = Instant :: now ( ) ;
213223 loop {
214224 peer_manager. process_events ( ) ;
215225 channel_manager. process_pending_events ( & event_handler) ;
@@ -247,6 +257,14 @@ impl BackgroundProcessor {
247257 peer_manager. timer_tick_occurred ( ) ;
248258 last_ping_call = Instant :: now ( ) ;
249259 }
260+
261+ if last_prune_call. elapsed ( ) . as_secs ( ) > NETWORK_PRUNE_TIMER {
262+ if let Some ( ref handler) = net_graph_msg_handler {
263+ log_trace ! ( logger, "Pruning network graph of stale entries" ) ;
264+ ( * handler. deref ( ) ) . network_graph ( ) . remove_stale_channels ( ) ;
265+ last_prune_call = Instant :: now ( ) ;
266+ }
267+ }
250268 }
251269 } ) ;
252270 Self { stop_thread : stop_thread_clone, thread_handle : Some ( handle) }
0 commit comments