@@ -34,6 +34,8 @@ use std::ops::Deref;
34
34
/// [`ChannelManager`] persistence should be done in the background.
35
35
/// * Calling [`ChannelManager::timer_tick_occurred`] and [`PeerManager::timer_tick_occurred`]
36
36
/// at the appropriate intervals.
37
+ /// * Calling [`NetworkGraph::remove_stale_channels`] (if a [`NetGraphMsgHandler`] is provided to
38
+ /// [`BackgroundProcessor::start`]).
37
39
///
38
40
/// It will also call [`PeerManager::process_events`] periodically though this shouldn't be relied
39
41
/// upon as doing so may result in high latency.
@@ -68,6 +70,9 @@ const PING_TIMER: u64 = 30;
68
70
#[ cfg( test) ]
69
71
const PING_TIMER : u64 = 1 ;
70
72
73
+ /// Prune the network graph of stale entries hourly.
74
+ const NETWORK_PRUNE_TIMER : u64 = 60 * 60 ;
75
+
71
76
/// Trait which handles persisting a [`ChannelManager`] to disk.
72
77
///
73
78
/// [`ChannelManager`]: lightning::ln::channelmanager::ChannelManager
@@ -203,13 +208,16 @@ impl BackgroundProcessor {
203
208
let stop_thread = Arc :: new ( AtomicBool :: new ( false ) ) ;
204
209
let stop_thread_clone = stop_thread. clone ( ) ;
205
210
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 ( ) ) } ;
207
212
208
213
log_trace ! ( logger, "Calling ChannelManager's timer_tick_occurred on startup" ) ;
209
214
channel_manager. timer_tick_occurred ( ) ;
210
215
211
216
let mut last_freshness_call = Instant :: now ( ) ;
212
217
let mut last_ping_call = Instant :: now ( ) ;
218
+ let mut last_prune_call = Instant :: now ( ) ;
219
+ let mut have_pruned = false ;
220
+
213
221
loop {
214
222
peer_manager. process_events ( ) ;
215
223
channel_manager. process_pending_events ( & event_handler) ;
@@ -247,6 +255,19 @@ impl BackgroundProcessor {
247
255
peer_manager. timer_tick_occurred ( ) ;
248
256
last_ping_call = Instant :: now ( ) ;
249
257
}
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
+ }
250
271
}
251
272
} ) ;
252
273
Self { stop_thread : stop_thread_clone, thread_handle : Some ( handle) }
0 commit comments