Skip to content

Commit d308710

Browse files
committed
Use flexible timer types in background processor's regular jobs
`background-processor` does a number of jobs on various timers. Currently, those are all done by checking the timers every 100ms by interrogating `std::time::Instant`. This is fine for the threaded version, but we'd like more flexibility in the `futures`- based `background-processor`. Here we swap the `std::time::Instant` interrogation for a lambda which we will switch out to the user-provided sleeper in the next commit.
1 parent ce6bcf6 commit d308710

File tree

1 file changed

+20
-19
lines changed
  • lightning-background-processor/src

1 file changed

+20
-19
lines changed

lightning-background-processor/src/lib.rs

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -207,15 +207,15 @@ macro_rules! define_run_body {
207207
($persister: ident, $chain_monitor: ident, $process_chain_monitor_events: expr,
208208
$channel_manager: ident, $process_channel_manager_events: expr,
209209
$gossip_sync: ident, $peer_manager: ident, $logger: ident, $scorer: ident,
210-
$loop_exit_check: expr, $await: expr)
210+
$loop_exit_check: expr, $await: expr, $get_timer: expr, $timer_elapsed: expr)
211211
=> { {
212212
log_trace!($logger, "Calling ChannelManager's timer_tick_occurred on startup");
213213
$channel_manager.timer_tick_occurred();
214214

215-
let mut last_freshness_call = Instant::now();
216-
let mut last_ping_call = Instant::now();
217-
let mut last_prune_call = Instant::now();
218-
let mut last_scorer_persist_call = Instant::now();
215+
let mut last_freshness_call = $get_timer(FRESHNESS_TIMER);
216+
let mut last_ping_call = $get_timer(PING_TIMER);
217+
let mut last_prune_call = $get_timer(FIRST_NETWORK_PRUNE_TIMER);
218+
let mut last_scorer_persist_call = $get_timer(SCORER_PERSIST_TIMER);
219219
let mut have_pruned = false;
220220

221221
loop {
@@ -237,9 +237,9 @@ macro_rules! define_run_body {
237237

238238
// We wait up to 100ms, but track how long it takes to detect being put to sleep,
239239
// see `await_start`'s use below.
240-
let await_start = Instant::now();
240+
let mut await_start = $get_timer(1);
241241
let updates_available = $await;
242-
let await_time = await_start.elapsed();
242+
let await_slow = $timer_elapsed(&mut await_start, 1);
243243

244244
if updates_available {
245245
log_trace!($logger, "Persisting ChannelManager...");
@@ -251,12 +251,12 @@ macro_rules! define_run_body {
251251
log_trace!($logger, "Terminating background processor.");
252252
break;
253253
}
254-
if last_freshness_call.elapsed().as_secs() > FRESHNESS_TIMER {
254+
if $timer_elapsed(&mut last_freshness_call, FRESHNESS_TIMER) {
255255
log_trace!($logger, "Calling ChannelManager's timer_tick_occurred");
256256
$channel_manager.timer_tick_occurred();
257-
last_freshness_call = Instant::now();
257+
last_freshness_call = $get_timer(FRESHNESS_TIMER);
258258
}
259-
if await_time > Duration::from_secs(1) {
259+
if await_slow {
260260
// On various platforms, we may be starved of CPU cycles for several reasons.
261261
// E.g. on iOS, if we've been in the background, we will be entirely paused.
262262
// Similarly, if we're on a desktop platform and the device has been asleep, we
@@ -271,18 +271,18 @@ macro_rules! define_run_body {
271271
// peers.
272272
log_trace!($logger, "100ms sleep took more than a second, disconnecting peers.");
273273
$peer_manager.disconnect_all_peers();
274-
last_ping_call = Instant::now();
275-
} else if last_ping_call.elapsed().as_secs() > PING_TIMER {
274+
last_ping_call = $get_timer(PING_TIMER);
275+
} else if $timer_elapsed(&mut last_ping_call, PING_TIMER) {
276276
log_trace!($logger, "Calling PeerManager's timer_tick_occurred");
277277
$peer_manager.timer_tick_occurred();
278-
last_ping_call = Instant::now();
278+
last_ping_call = $get_timer(PING_TIMER);
279279
}
280280

281281
// Note that we want to run a graph prune once not long after startup before
282282
// falling back to our usual hourly prunes. This avoids short-lived clients never
283283
// pruning their network graph. We run once 60 seconds after startup before
284284
// continuing our normal cadence.
285-
if last_prune_call.elapsed().as_secs() > if have_pruned { NETWORK_PRUNE_TIMER } else { FIRST_NETWORK_PRUNE_TIMER } {
285+
if $timer_elapsed(&mut last_prune_call, if have_pruned { NETWORK_PRUNE_TIMER } else { FIRST_NETWORK_PRUNE_TIMER }) {
286286
// The network graph must not be pruned while rapid sync completion is pending
287287
if let Some(network_graph) = $gossip_sync.prunable_network_graph() {
288288
log_trace!($logger, "Pruning and persisting network graph.");
@@ -292,19 +292,19 @@ macro_rules! define_run_body {
292292
log_error!($logger, "Error: Failed to persist network graph, check your disk and permissions {}", e)
293293
}
294294

295-
last_prune_call = Instant::now();
295+
last_prune_call = $get_timer(NETWORK_PRUNE_TIMER);
296296
have_pruned = true;
297297
}
298298
}
299299

300-
if last_scorer_persist_call.elapsed().as_secs() > SCORER_PERSIST_TIMER {
300+
if $timer_elapsed(&mut last_scorer_persist_call, SCORER_PERSIST_TIMER) {
301301
if let Some(ref scorer) = $scorer {
302302
log_trace!($logger, "Persisting scorer");
303303
if let Err(e) = $persister.persist_scorer(&scorer) {
304304
log_error!($logger, "Error: Failed to persist scorer, check your disk and permissions {}", e)
305305
}
306306
}
307-
last_scorer_persist_call = Instant::now();
307+
last_scorer_persist_call = $get_timer(SCORER_PERSIST_TIMER);
308308
}
309309
}
310310

@@ -411,7 +411,7 @@ where
411411
false
412412
}
413413
}
414-
})
414+
}, |_| Instant::now(), |time: &Instant, dur| time.elapsed().as_secs() > dur)
415415
}
416416

417417
impl BackgroundProcessor {
@@ -522,7 +522,8 @@ impl BackgroundProcessor {
522522
define_run_body!(persister, chain_monitor, chain_monitor.process_pending_events(&event_handler),
523523
channel_manager, channel_manager.process_pending_events(&event_handler),
524524
gossip_sync, peer_manager, logger, scorer, stop_thread.load(Ordering::Acquire),
525-
channel_manager.await_persistable_update_timeout(Duration::from_millis(100)))
525+
channel_manager.await_persistable_update_timeout(Duration::from_millis(100)),
526+
|_| Instant::now(), |time: &Instant, dur| time.elapsed().as_secs() > dur)
526527
});
527528
Self { stop_thread: stop_thread_clone, thread_handle: Some(handle) }
528529
}

0 commit comments

Comments
 (0)