@@ -116,6 +116,13 @@ const FIRST_NETWORK_PRUNE_TIMER: u64 = 60;
116
116
#[ cfg( test) ]
117
117
const FIRST_NETWORK_PRUNE_TIMER : u64 = 1 ;
118
118
119
+ #[ cfg( feature = "futures" ) ]
120
+ /// core::cmp::min is not currently const, so we define a trivial (and equivalent) replacement
121
+ const fn min_u64 ( a : u64 , b : u64 ) -> u64 { if a < b { a } else { b } }
122
+ #[ cfg( feature = "futures" ) ]
123
+ const FASTEST_TIMER : u64 = min_u64 ( min_u64 ( FRESHNESS_TIMER , PING_TIMER ) ,
124
+ min_u64 ( SCORER_PERSIST_TIMER , FIRST_NETWORK_PRUNE_TIMER ) ) ;
125
+
119
126
/// Either [`P2PGossipSync`] or [`RapidGossipSync`].
120
127
pub enum GossipSync <
121
128
P : Deref < Target = P2PGossipSync < G , U , L > > ,
@@ -258,7 +265,8 @@ macro_rules! define_run_body {
258
265
( $persister: ident, $chain_monitor: ident, $process_chain_monitor_events: expr,
259
266
$channel_manager: ident, $process_channel_manager_events: expr,
260
267
$gossip_sync: ident, $peer_manager: ident, $logger: ident, $scorer: ident,
261
- $loop_exit_check: expr, $await: expr, $get_timer: expr, $timer_elapsed: expr)
268
+ $loop_exit_check: expr, $await: expr, $get_timer: expr, $timer_elapsed: expr,
269
+ $check_slow_await: expr)
262
270
=> { {
263
271
log_trace!( $logger, "Calling ChannelManager's timer_tick_occurred on startup" ) ;
264
272
$channel_manager. timer_tick_occurred( ) ;
@@ -288,9 +296,10 @@ macro_rules! define_run_body {
288
296
289
297
// We wait up to 100ms, but track how long it takes to detect being put to sleep,
290
298
// see `await_start`'s use below.
291
- let mut await_start = $get_timer( 1 ) ;
299
+ let mut await_start = None ;
300
+ if $check_slow_await { await_start = Some ( $get_timer( 1 ) ) ; }
292
301
let updates_available = $await;
293
- let await_slow = $ timer_elapsed( & mut await_start, 1 ) ;
302
+ let await_slow = if $check_slow_await { $ timer_elapsed( & mut await_start. unwrap ( ) , 1 ) } else { false } ;
294
303
295
304
if updates_available {
296
305
log_trace!( $logger, "Persisting ChannelManager..." ) ;
@@ -449,6 +458,11 @@ use core::task;
449
458
/// feature, doing so will skip calling [`NetworkGraph::remove_stale_channels_and_tracking`],
450
459
/// you should call [`NetworkGraph::remove_stale_channels_and_tracking_with_time`] regularly
451
460
/// manually instead.
461
+ ///
462
+ /// The `mobile_interruptable_platform` flag should be set if we're currently running on a
463
+ /// mobile device, where we may need to check for interruption of the application regularly. If you
464
+ /// are unsure, you should set the flag, as the performance impact of it is minimal unless there
465
+ /// are hundreds or thousands of simultaneous process calls running.
452
466
#[ cfg( feature = "futures" ) ]
453
467
pub async fn process_events_async <
454
468
' a ,
@@ -484,7 +498,7 @@ pub async fn process_events_async<
484
498
> (
485
499
persister : PS , event_handler : EventHandler , chain_monitor : M , channel_manager : CM ,
486
500
gossip_sync : GossipSync < PGS , RGS , G , UL , L > , peer_manager : PM , logger : L , scorer : Option < S > ,
487
- sleeper : Sleeper ,
501
+ sleeper : Sleeper , mobile_interruptable_platform : bool ,
488
502
) -> Result < ( ) , lightning:: io:: Error >
489
503
where
490
504
UL :: Target : ' static + UtxoLookup ,
@@ -526,7 +540,7 @@ where
526
540
let fut = Selector {
527
541
a: channel_manager. get_persistable_update_future( ) ,
528
542
b: chain_monitor. get_update_future( ) ,
529
- c: sleeper( Duration :: from_millis( 100 ) ) ,
543
+ c: sleeper( if mobile_interruptable_platform { Duration :: from_millis( 100 ) } else { Duration :: from_secs ( FASTEST_TIMER ) } ) ,
530
544
} ;
531
545
match fut. await {
532
546
SelectorOutput :: A => true ,
@@ -541,7 +555,7 @@ where
541
555
let mut waker = dummy_waker( ) ;
542
556
let mut ctx = task:: Context :: from_waker( & mut waker) ;
543
557
core:: pin:: Pin :: new( fut) . poll( & mut ctx) . is_ready( )
544
- } )
558
+ } , mobile_interruptable_platform )
545
559
}
546
560
547
561
#[ cfg( feature = "std" ) ]
@@ -660,7 +674,7 @@ impl BackgroundProcessor {
660
674
channel_manager. get_persistable_update_future( ) ,
661
675
chain_monitor. get_update_future( )
662
676
) . wait_timeout( Duration :: from_millis( 100 ) ) ,
663
- |_| Instant :: now( ) , |time: & Instant , dur| time. elapsed( ) . as_secs( ) > dur)
677
+ |_| Instant :: now( ) , |time: & Instant , dur| time. elapsed( ) . as_secs( ) > dur, false )
664
678
} ) ;
665
679
Self { stop_thread : stop_thread_clone, thread_handle : Some ( handle) }
666
680
}
0 commit comments