@@ -26,6 +26,10 @@ extern crate alloc;
2626extern crate lightning;
2727extern crate lightning_rapid_gossip_sync;
2828
29+ mod fwd_batch;
30+
31+ use fwd_batch:: BatchDelay ;
32+
2933use lightning:: chain;
3034use lightning:: chain:: chaininterface:: { BroadcasterInterface , FeeEstimator } ;
3135use lightning:: chain:: chainmonitor:: { ChainMonitor , Persist } ;
@@ -328,7 +332,7 @@ macro_rules! define_run_body {
328332 $peer_manager: ident, $gossip_sync: ident,
329333 $process_sweeper: expr,
330334 $logger: ident, $scorer: ident, $loop_exit_check: expr, $await: expr, $get_timer: expr,
331- $timer_elapsed: expr, $check_slow_await: expr, $time_fetch: expr,
335+ $timer_elapsed: expr, $check_slow_await: expr, $time_fetch: expr, $batch_delay : expr ,
332336 ) => { {
333337 log_trace!( $logger, "Calling ChannelManager's timer_tick_occurred on startup" ) ;
334338 $channel_manager. get_cm( ) . timer_tick_occurred( ) ;
@@ -345,6 +349,9 @@ macro_rules! define_run_body {
345349 let mut have_pruned = false ;
346350 let mut have_decayed_scorer = false ;
347351
352+ let mut cur_batch_delay = $batch_delay. get( ) ;
353+ let mut last_forwards_processing_call = $get_timer( cur_batch_delay) ;
354+
348355 loop {
349356 $process_channel_manager_events;
350357 $process_chain_monitor_events;
@@ -369,6 +376,18 @@ macro_rules! define_run_body {
369376 break ;
370377 }
371378
379+ if $timer_elapsed( & mut last_forwards_processing_call, cur_batch_delay) {
380+ $channel_manager. get_cm( ) . process_pending_htlc_forwards( ) ;
381+ cur_batch_delay = $batch_delay. next( ) ;
382+ last_forwards_processing_call = $get_timer( cur_batch_delay) ;
383+ }
384+
385+ // Exit the loop if the background processor was requested to stop.
386+ if $loop_exit_check {
387+ log_trace!( $logger, "Terminating background processor." ) ;
388+ break ;
389+ }
390+
372391 // We wait up to 100ms, but track how long it takes to detect being put to sleep,
373392 // see `await_start`'s use below.
374393 let mut await_start = None ;
@@ -523,12 +542,14 @@ pub(crate) mod futures_util {
523542 C : Future < Output = ( ) > + Unpin ,
524543 D : Future < Output = ( ) > + Unpin ,
525544 E : Future < Output = bool > + Unpin ,
545+ F : Future < Output = bool > + Unpin ,
526546 > {
527547 pub a : A ,
528548 pub b : B ,
529549 pub c : C ,
530550 pub d : D ,
531551 pub e : E ,
552+ pub f : F ,
532553 }
533554
534555 pub ( crate ) enum SelectorOutput {
@@ -537,6 +558,7 @@ pub(crate) mod futures_util {
537558 C ,
538559 D ,
539560 E ( bool ) ,
561+ F ( bool ) ,
540562 }
541563
542564 impl <
@@ -545,7 +567,8 @@ pub(crate) mod futures_util {
545567 C : Future < Output = ( ) > + Unpin ,
546568 D : Future < Output = ( ) > + Unpin ,
547569 E : Future < Output = bool > + Unpin ,
548- > Future for Selector < A , B , C , D , E >
570+ F : Future < Output = bool > + Unpin ,
571+ > Future for Selector < A , B , C , D , E , F >
549572 {
550573 type Output = SelectorOutput ;
551574 fn poll (
@@ -581,6 +604,12 @@ pub(crate) mod futures_util {
581604 } ,
582605 Poll :: Pending => { } ,
583606 }
607+ match Pin :: new ( & mut self . f ) . poll ( ctx) {
608+ Poll :: Ready ( res) => {
609+ return Poll :: Ready ( SelectorOutput :: F ( res) ) ;
610+ } ,
611+ Poll :: Pending => { } ,
612+ }
584613 Poll :: Pending
585614 }
586615 }
@@ -863,6 +892,7 @@ where
863892 event_handler ( event) . await
864893 } )
865894 } ;
895+ let batch_delay = Arc :: new ( BatchDelay :: new ( ) ) ;
866896 define_run_body ! (
867897 persister,
868898 chain_monitor,
@@ -901,7 +931,8 @@ where
901931 b: chain_monitor. get_update_future( ) ,
902932 c: om_fut,
903933 d: lm_fut,
904- e: sleeper( if mobile_interruptable_platform {
934+ e: sleeper( batch_delay. get( ) ) ,
935+ f: sleeper( if mobile_interruptable_platform {
905936 Duration :: from_millis( 100 )
906937 } else {
907938 FASTEST_TIMER
@@ -912,6 +943,9 @@ where
912943 SelectorOutput :: E ( exit) => {
913944 should_break = exit;
914945 } ,
946+ SelectorOutput :: F ( exit) => {
947+ should_break = exit;
948+ } ,
915949 }
916950 } ,
917951 |t| sleeper( t) ,
@@ -928,6 +962,7 @@ where
928962 } ,
929963 mobile_interruptable_platform,
930964 fetch_time,
965+ batch_delay,
931966 )
932967}
933968
@@ -1051,6 +1086,7 @@ impl BackgroundProcessor {
10511086 }
10521087 event_handler. handle_event ( event)
10531088 } ;
1089+ let batch_delay = Arc :: new ( BatchDelay :: new ( ) ) ;
10541090 define_run_body ! (
10551091 persister,
10561092 chain_monitor,
@@ -1094,7 +1130,9 @@ impl BackgroundProcessor {
10941130 & chain_monitor. get_update_future( ) ,
10951131 ) ,
10961132 } ;
1097- sleeper. wait_timeout( Duration :: from_millis( 100 ) ) ;
1133+ let batch_delay = batch_delay. get( ) ;
1134+ let fastest_timeout = batch_delay. min( Duration :: from_millis( 100 ) ) ;
1135+ sleeper. wait_timeout( fastest_timeout) ;
10981136 } ,
10991137 |_| Instant :: now( ) ,
11001138 |time: & Instant , dur| time. elapsed( ) > dur,
@@ -1107,6 +1145,7 @@ impl BackgroundProcessor {
11071145 . expect( "Time should be sometime after 1970" ) ,
11081146 )
11091147 } ,
1148+ batch_delay,
11101149 )
11111150 } ) ;
11121151 Self { stop_thread : stop_thread_clone, thread_handle : Some ( handle) }
0 commit comments