Skip to content

Commit 03537cc

Browse files
authored
Merge pull request #1035 from TheBlueMatt/2021-08-faster-pings
Suggest faster ping in `PeerManager::timer_tick_occurred` docs
2 parents 8530078 + 0f1a3b1 commit 03537cc

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

lightning-background-processor/src/lib.rs

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ const FRESHNESS_TIMER: u64 = 60;
5050
#[cfg(test)]
5151
const FRESHNESS_TIMER: u64 = 1;
5252

53+
const PING_TIMER: u64 = 5;
54+
5355
/// Trait which handles persisting a [`ChannelManager`] to disk.
5456
///
5557
/// [`ChannelManager`]: lightning::ln::channelmanager::ChannelManager
@@ -138,7 +140,8 @@ impl BackgroundProcessor {
138140
let stop_thread = Arc::new(AtomicBool::new(false));
139141
let stop_thread_clone = stop_thread.clone();
140142
let handle = thread::spawn(move || -> Result<(), std::io::Error> {
141-
let mut current_time = Instant::now();
143+
let mut last_freshness_call = Instant::now();
144+
let mut last_ping_call = Instant::now();
142145
loop {
143146
peer_manager.process_events();
144147
channel_manager.process_pending_events(&event_handler);
@@ -153,11 +156,27 @@ impl BackgroundProcessor {
153156
log_trace!(logger, "Terminating background processor.");
154157
return Ok(());
155158
}
156-
if current_time.elapsed().as_secs() > FRESHNESS_TIMER {
157-
log_trace!(logger, "Calling ChannelManager's and PeerManager's timer_tick_occurred");
159+
if last_freshness_call.elapsed().as_secs() > FRESHNESS_TIMER {
160+
log_trace!(logger, "Calling ChannelManager's timer_tick_occurred");
158161
channel_manager.timer_tick_occurred();
162+
last_freshness_call = Instant::now();
163+
}
164+
if last_ping_call.elapsed().as_secs() > PING_TIMER * 2 {
165+
// On various platforms, we may be starved of CPU cycles for several reasons.
166+
// E.g. on iOS, if we've been in the background, we will be entirely paused.
167+
// Similarly, if we're on a desktop platform and the device has been asleep, we
168+
// may not get any cycles.
169+
// In any case, if we've been entirely paused for more than double our ping
170+
// timer, we should have disconnected all sockets by now (and they're probably
171+
// dead anyway), so disconnect them by calling `timer_tick_occurred()` twice.
172+
log_trace!(logger, "Awoke after more than double our ping timer, disconnecting peers.");
173+
peer_manager.timer_tick_occurred();
174+
peer_manager.timer_tick_occurred();
175+
last_ping_call = Instant::now();
176+
} else if last_ping_call.elapsed().as_secs() > PING_TIMER {
177+
log_trace!(logger, "Calling PeerManager's timer_tick_occurred");
159178
peer_manager.timer_tick_occurred();
160-
current_time = Instant::now();
179+
last_ping_call = Instant::now();
161180
}
162181
}
163182
});
@@ -441,8 +460,10 @@ mod tests {
441460
let bg_processor = BackgroundProcessor::start(persister, event_handler, nodes[0].chain_monitor.clone(), nodes[0].node.clone(), nodes[0].peer_manager.clone(), nodes[0].logger.clone());
442461
loop {
443462
let log_entries = nodes[0].logger.lines.lock().unwrap();
444-
let desired_log = "Calling ChannelManager's and PeerManager's timer_tick_occurred".to_string();
445-
if log_entries.get(&("lightning_background_processor".to_string(), desired_log)).is_some() {
463+
let desired_log = "Calling ChannelManager's timer_tick_occurred".to_string();
464+
let second_desired_log = "Calling PeerManager's timer_tick_occurred".to_string();
465+
if log_entries.get(&("lightning_background_processor".to_string(), desired_log)).is_some() &&
466+
log_entries.get(&("lightning_background_processor".to_string(), second_desired_log)).is_some() {
446467
break
447468
}
448469
}

lightning/src/ln/peer_handler.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1372,9 +1372,12 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref> PeerManager<D
13721372
}
13731373
}
13741374

1375-
/// This function should be called roughly once every 30 seconds.
1376-
/// It will send pings to each peer and disconnect those which did not respond to the last
1377-
/// round of pings.
1375+
/// Send pings to each peer and disconnect those which did not respond to the last round of
1376+
/// pings.
1377+
///
1378+
/// This may be called on any timescale you want, however, roughly once every five to ten
1379+
/// seconds is preferred. The call rate determines both how often we send a ping to our peers
1380+
/// and how much time they have to respond before we disconnect them.
13781381
///
13791382
/// May call [`send_data`] on all [`SocketDescriptor`]s. Thus, be very careful with reentrancy
13801383
/// issues!

0 commit comments

Comments
 (0)