@@ -26,6 +26,10 @@ extern crate alloc;
26
26
extern crate lightning;
27
27
extern crate lightning_rapid_gossip_sync;
28
28
29
+ mod fwd_batch;
30
+
31
+ use fwd_batch:: BatchDelay ;
32
+
29
33
use lightning:: chain;
30
34
use lightning:: chain:: chaininterface:: { BroadcasterInterface , FeeEstimator } ;
31
35
use lightning:: chain:: chainmonitor:: { ChainMonitor , Persist } ;
@@ -328,7 +332,7 @@ macro_rules! define_run_body {
328
332
$peer_manager: ident, $gossip_sync: ident,
329
333
$process_sweeper: expr,
330
334
$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 ,
332
336
) => { {
333
337
log_trace!( $logger, "Calling ChannelManager's timer_tick_occurred on startup" ) ;
334
338
$channel_manager. get_cm( ) . timer_tick_occurred( ) ;
@@ -345,6 +349,9 @@ macro_rules! define_run_body {
345
349
let mut have_pruned = false ;
346
350
let mut have_decayed_scorer = false ;
347
351
352
+ let mut cur_batch_delay = $batch_delay. get( ) ;
353
+ let mut last_forwards_processing_call = $get_timer( cur_batch_delay) ;
354
+
348
355
loop {
349
356
$process_channel_manager_events;
350
357
$process_chain_monitor_events;
@@ -369,6 +376,18 @@ macro_rules! define_run_body {
369
376
break ;
370
377
}
371
378
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
+
372
391
// We wait up to 100ms, but track how long it takes to detect being put to sleep,
373
392
// see `await_start`'s use below.
374
393
let mut await_start = None ;
@@ -523,12 +542,14 @@ pub(crate) mod futures_util {
523
542
C : Future < Output = ( ) > + Unpin ,
524
543
D : Future < Output = ( ) > + Unpin ,
525
544
E : Future < Output = bool > + Unpin ,
545
+ F : Future < Output = bool > + Unpin ,
526
546
> {
527
547
pub a : A ,
528
548
pub b : B ,
529
549
pub c : C ,
530
550
pub d : D ,
531
551
pub e : E ,
552
+ pub f : F ,
532
553
}
533
554
534
555
pub ( crate ) enum SelectorOutput {
@@ -537,6 +558,7 @@ pub(crate) mod futures_util {
537
558
C ,
538
559
D ,
539
560
E ( bool ) ,
561
+ F ( bool ) ,
540
562
}
541
563
542
564
impl <
@@ -545,7 +567,8 @@ pub(crate) mod futures_util {
545
567
C : Future < Output = ( ) > + Unpin ,
546
568
D : Future < Output = ( ) > + Unpin ,
547
569
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 >
549
572
{
550
573
type Output = SelectorOutput ;
551
574
fn poll (
@@ -581,6 +604,12 @@ pub(crate) mod futures_util {
581
604
} ,
582
605
Poll :: Pending => { } ,
583
606
}
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
+ }
584
613
Poll :: Pending
585
614
}
586
615
}
@@ -863,6 +892,7 @@ where
863
892
event_handler ( event) . await
864
893
} )
865
894
} ;
895
+ let batch_delay = Arc :: new ( BatchDelay :: new ( ) ) ;
866
896
define_run_body ! (
867
897
persister,
868
898
chain_monitor,
@@ -901,7 +931,8 @@ where
901
931
b: chain_monitor. get_update_future( ) ,
902
932
c: om_fut,
903
933
d: lm_fut,
904
- e: sleeper( if mobile_interruptable_platform {
934
+ e: sleeper( batch_delay. get( ) ) ,
935
+ f: sleeper( if mobile_interruptable_platform {
905
936
Duration :: from_millis( 100 )
906
937
} else {
907
938
FASTEST_TIMER
@@ -912,6 +943,9 @@ where
912
943
SelectorOutput :: E ( exit) => {
913
944
should_break = exit;
914
945
} ,
946
+ SelectorOutput :: F ( exit) => {
947
+ should_break = exit;
948
+ } ,
915
949
}
916
950
} ,
917
951
|t| sleeper( t) ,
@@ -928,6 +962,7 @@ where
928
962
} ,
929
963
mobile_interruptable_platform,
930
964
fetch_time,
965
+ batch_delay,
931
966
)
932
967
}
933
968
@@ -1051,6 +1086,7 @@ impl BackgroundProcessor {
1051
1086
}
1052
1087
event_handler. handle_event ( event)
1053
1088
} ;
1089
+ let batch_delay = Arc :: new ( BatchDelay :: new ( ) ) ;
1054
1090
define_run_body ! (
1055
1091
persister,
1056
1092
chain_monitor,
@@ -1094,7 +1130,9 @@ impl BackgroundProcessor {
1094
1130
& chain_monitor. get_update_future( ) ,
1095
1131
) ,
1096
1132
} ;
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) ;
1098
1136
} ,
1099
1137
|_| Instant :: now( ) ,
1100
1138
|time: & Instant , dur| time. elapsed( ) > dur,
@@ -1107,6 +1145,7 @@ impl BackgroundProcessor {
1107
1145
. expect( "Time should be sometime after 1970" ) ,
1108
1146
)
1109
1147
} ,
1148
+ batch_delay,
1110
1149
)
1111
1150
} ) ;
1112
1151
Self { stop_thread : stop_thread_clone, thread_handle : Some ( handle) }
0 commit comments