@@ -50,6 +50,8 @@ const FRESHNESS_TIMER: u64 = 60;
50
50
#[ cfg( test) ]
51
51
const FRESHNESS_TIMER : u64 = 1 ;
52
52
53
+ const PING_TIMER : u64 = 5 ;
54
+
53
55
/// Trait which handles persisting a [`ChannelManager`] to disk.
54
56
///
55
57
/// [`ChannelManager`]: lightning::ln::channelmanager::ChannelManager
@@ -138,7 +140,8 @@ impl BackgroundProcessor {
138
140
let stop_thread = Arc :: new ( AtomicBool :: new ( false ) ) ;
139
141
let stop_thread_clone = stop_thread. clone ( ) ;
140
142
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 ( ) ;
142
145
loop {
143
146
peer_manager. process_events ( ) ;
144
147
channel_manager. process_pending_events ( & event_handler) ;
@@ -153,11 +156,27 @@ impl BackgroundProcessor {
153
156
log_trace ! ( logger, "Terminating background processor." ) ;
154
157
return Ok ( ( ) ) ;
155
158
}
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" ) ;
158
161
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" ) ;
159
178
peer_manager. timer_tick_occurred ( ) ;
160
- current_time = Instant :: now ( ) ;
179
+ last_ping_call = Instant :: now ( ) ;
161
180
}
162
181
}
163
182
} ) ;
@@ -441,8 +460,10 @@ mod tests {
441
460
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 ( ) ) ;
442
461
loop {
443
462
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 ( ) {
446
467
break
447
468
}
448
469
}
0 commit comments