Skip to content

Commit 68de973

Browse files
committed
f rewrite to just check the await time
1 parent 4fdba1c commit 68de973

File tree

1 file changed

+8
-11
lines changed
  • lightning-background-processor/src

1 file changed

+8
-11
lines changed

lightning-background-processor/src/lib.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -218,24 +218,23 @@ impl BackgroundProcessor {
218218
let mut last_prune_call = Instant::now();
219219
let mut have_pruned = false;
220220

221-
// When considering how long its taken since the last timer tick, we don't want to
222-
// count any time spent in user code, especially since event processing can block on
223-
// disk writes. Thus, we track how long we spent in event handling here.
224-
let mut ev_handle_time_since_last_ping = Duration::from_millis(0);
225221
loop {
226-
let ev_handle_start = Instant::now();
227222
peer_manager.process_events(); // Note that this may block on ChannelManager's locking
228223
channel_manager.process_pending_events(&event_handler);
229224
chain_monitor.process_pending_events(&event_handler);
230-
ev_handle_time_since_last_ping += ev_handle_start.elapsed();
225+
226+
// We wait up to 100ms, but on a mobile device without a ton of activity, if we get
227+
// put in the background and paused, this is likely where it will happen, causing
228+
// the wait time to be substantially longer.
229+
let await_start = Instant::now();
231230
let updates_available =
232231
channel_manager.await_persistable_update_timeout(Duration::from_millis(100));
232+
let await_time = await_start.elapsed();
233+
233234
if updates_available {
234-
let persist_start = Instant::now();
235235
log_trace!(logger, "Persisting ChannelManager...");
236236
persister.persist_manager(&*channel_manager)?;
237237
log_trace!(logger, "Done persisting ChannelManager.");
238-
ev_handle_time_since_last_ping += persist_start.elapsed();
239238
}
240239
// Exit the loop if the background processor was requested to stop.
241240
if stop_thread.load(Ordering::Acquire) == true {
@@ -247,7 +246,7 @@ impl BackgroundProcessor {
247246
channel_manager.timer_tick_occurred();
248247
last_freshness_call = Instant::now();
249248
}
250-
if (last_ping_call.elapsed() - ev_handle_time_since_last_ping).as_secs() > PING_TIMER * 2 {
249+
if await_time > Duration::from_secs(1) {
251250
// On various platforms, we may be starved of CPU cycles for several reasons.
252251
// E.g. on iOS, if we've been in the background, we will be entirely paused.
253252
// Similarly, if we're on a desktop platform and the device has been asleep, we
@@ -263,12 +262,10 @@ impl BackgroundProcessor {
263262
log_trace!(logger, "Awoke after more than double our ping timer, disconnecting peers.");
264263
peer_manager.disconnect_all_peers();
265264
last_ping_call = Instant::now();
266-
ev_handle_time_since_last_ping = Duration::from_millis(0);
267265
} else if last_ping_call.elapsed().as_secs() > PING_TIMER {
268266
log_trace!(logger, "Calling PeerManager's timer_tick_occurred");
269267
peer_manager.timer_tick_occurred();
270268
last_ping_call = Instant::now();
271-
ev_handle_time_since_last_ping = Duration::from_millis(0);
272269
}
273270

274271
// Note that we want to run a graph prune once not long after startup before

0 commit comments

Comments
 (0)