@@ -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,19 @@ 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
+ // Checke whether to exit the loop again, as some time might have passed since we
386
+ // checked above.
387
+ if $loop_exit_check {
388
+ log_trace!( $logger, "Terminating background processor." ) ;
389
+ break ;
390
+ }
391
+
372
392
// We wait up to 100ms, but track how long it takes to detect being put to sleep,
373
393
// see `await_start`'s use below.
374
394
let mut await_start = None ;
@@ -523,12 +543,14 @@ pub(crate) mod futures_util {
523
543
C : Future < Output = ( ) > + Unpin ,
524
544
D : Future < Output = ( ) > + Unpin ,
525
545
E : Future < Output = bool > + Unpin ,
546
+ F : Future < Output = bool > + Unpin ,
526
547
> {
527
548
pub a : A ,
528
549
pub b : B ,
529
550
pub c : C ,
530
551
pub d : D ,
531
552
pub e : E ,
553
+ pub f : F ,
532
554
}
533
555
534
556
pub ( crate ) enum SelectorOutput {
@@ -537,6 +559,7 @@ pub(crate) mod futures_util {
537
559
C ,
538
560
D ,
539
561
E ( bool ) ,
562
+ F ( bool ) ,
540
563
}
541
564
542
565
impl <
@@ -545,7 +568,8 @@ pub(crate) mod futures_util {
545
568
C : Future < Output = ( ) > + Unpin ,
546
569
D : Future < Output = ( ) > + Unpin ,
547
570
E : Future < Output = bool > + Unpin ,
548
- > Future for Selector < A , B , C , D , E >
571
+ F : Future < Output = bool > + Unpin ,
572
+ > Future for Selector < A , B , C , D , E , F >
549
573
{
550
574
type Output = SelectorOutput ;
551
575
fn poll (
@@ -581,6 +605,12 @@ pub(crate) mod futures_util {
581
605
} ,
582
606
Poll :: Pending => { } ,
583
607
}
608
+ match Pin :: new ( & mut self . f ) . poll ( ctx) {
609
+ Poll :: Ready ( res) => {
610
+ return Poll :: Ready ( SelectorOutput :: F ( res) ) ;
611
+ } ,
612
+ Poll :: Pending => { } ,
613
+ }
584
614
Poll :: Pending
585
615
}
586
616
}
@@ -863,6 +893,7 @@ where
863
893
event_handler ( event) . await
864
894
} )
865
895
} ;
896
+ let mut batch_delay = BatchDelay :: new ( ) ;
866
897
define_run_body ! (
867
898
persister,
868
899
chain_monitor,
@@ -901,7 +932,12 @@ where
901
932
b: chain_monitor. get_update_future( ) ,
902
933
c: om_fut,
903
934
d: lm_fut,
904
- e: sleeper( if mobile_interruptable_platform {
935
+ e: sleeper( if channel_manager. get_cm( ) . needs_pending_htlc_processing( ) {
936
+ batch_delay. get( )
937
+ } else {
938
+ Duration :: MAX
939
+ } ) ,
940
+ f: sleeper( if mobile_interruptable_platform {
905
941
Duration :: from_millis( 100 )
906
942
} else {
907
943
FASTEST_TIMER
@@ -912,6 +948,9 @@ where
912
948
SelectorOutput :: E ( exit) => {
913
949
should_break = exit;
914
950
} ,
951
+ SelectorOutput :: F ( exit) => {
952
+ should_break = exit;
953
+ } ,
915
954
}
916
955
} ,
917
956
|t| sleeper( t) ,
@@ -928,6 +967,7 @@ where
928
967
} ,
929
968
mobile_interruptable_platform,
930
969
fetch_time,
970
+ batch_delay,
931
971
)
932
972
}
933
973
@@ -1051,6 +1091,7 @@ impl BackgroundProcessor {
1051
1091
}
1052
1092
event_handler. handle_event ( event)
1053
1093
} ;
1094
+ let mut batch_delay = BatchDelay :: new ( ) ;
1054
1095
define_run_body ! (
1055
1096
persister,
1056
1097
chain_monitor,
@@ -1094,7 +1135,13 @@ impl BackgroundProcessor {
1094
1135
& chain_monitor. get_update_future( ) ,
1095
1136
) ,
1096
1137
} ;
1097
- sleeper. wait_timeout( Duration :: from_millis( 100 ) ) ;
1138
+ let batch_delay = if channel_manager. get_cm( ) . needs_pending_htlc_processing( ) {
1139
+ batch_delay. get( )
1140
+ } else {
1141
+ Duration :: MAX
1142
+ } ;
1143
+ let fastest_timeout = batch_delay. min( Duration :: from_millis( 100 ) ) ;
1144
+ sleeper. wait_timeout( fastest_timeout) ;
1098
1145
} ,
1099
1146
|_| Instant :: now( ) ,
1100
1147
|time: & Instant , dur| time. elapsed( ) > dur,
@@ -1107,6 +1154,7 @@ impl BackgroundProcessor {
1107
1154
. expect( "Time should be sometime after 1970" ) ,
1108
1155
)
1109
1156
} ,
1157
+ batch_delay,
1110
1158
)
1111
1159
} ) ;
1112
1160
Self { stop_thread : stop_thread_clone, thread_handle : Some ( handle) }
0 commit comments