@@ -492,23 +492,28 @@ pub(crate) mod futures_util {
492
492
pub ( crate ) struct Selector <
493
493
A : Future < Output = ( ) > + Unpin ,
494
494
B : Future < Output = ( ) > + Unpin ,
495
- C : Future < Output = bool > + Unpin ,
495
+ C : Future < Output = ( ) > + Unpin ,
496
+ D : Future < Output = bool > + Unpin ,
496
497
> {
497
498
pub a : A ,
498
499
pub b : B ,
499
500
pub c : C ,
501
+ pub d : D ,
500
502
}
503
+
501
504
pub ( crate ) enum SelectorOutput {
502
505
A ,
503
506
B ,
504
- C ( bool ) ,
507
+ C ,
508
+ D ( bool ) ,
505
509
}
506
510
507
511
impl <
508
512
A : Future < Output = ( ) > + Unpin ,
509
513
B : Future < Output = ( ) > + Unpin ,
510
- C : Future < Output = bool > + Unpin ,
511
- > Future for Selector < A , B , C >
514
+ C : Future < Output = ( ) > + Unpin ,
515
+ D : Future < Output = bool > + Unpin ,
516
+ > Future for Selector < A , B , C , D >
512
517
{
513
518
type Output = SelectorOutput ;
514
519
fn poll (
@@ -527,15 +532,43 @@ pub(crate) mod futures_util {
527
532
Poll :: Pending => { } ,
528
533
}
529
534
match Pin :: new ( & mut self . c ) . poll ( ctx) {
535
+ Poll :: Ready ( ( ) ) => {
536
+ return Poll :: Ready ( SelectorOutput :: C ) ;
537
+ } ,
538
+ Poll :: Pending => { } ,
539
+ }
540
+ match Pin :: new ( & mut self . d ) . poll ( ctx) {
530
541
Poll :: Ready ( res) => {
531
- return Poll :: Ready ( SelectorOutput :: C ( res) ) ;
542
+ return Poll :: Ready ( SelectorOutput :: D ( res) ) ;
532
543
} ,
533
544
Poll :: Pending => { } ,
534
545
}
535
546
Poll :: Pending
536
547
}
537
548
}
538
549
550
+ /// A selector that takes a future wrapped in an option that will be polled if it is `Some` and
551
+ /// will always be pending otherwise.
552
+ pub ( crate ) struct OptionalSelector < F : Future < Output = ( ) > + Unpin > {
553
+ pub optional_future : Option < F > ,
554
+ }
555
+
556
+ impl < F : Future < Output = ( ) > + Unpin > Future for OptionalSelector < F > {
557
+ type Output = ( ) ;
558
+ fn poll ( mut self : Pin < & mut Self > , ctx : & mut core:: task:: Context < ' _ > ) -> Poll < Self :: Output > {
559
+ match self . optional_future . as_mut ( ) {
560
+ Some ( f) => match Pin :: new ( f) . poll ( ctx) {
561
+ Poll :: Ready ( ( ) ) => {
562
+ self . optional_future . take ( ) ;
563
+ Poll :: Ready ( ( ) )
564
+ } ,
565
+ Poll :: Pending => Poll :: Pending ,
566
+ } ,
567
+ None => Poll :: Pending ,
568
+ }
569
+ }
570
+ }
571
+
539
572
// If we want to poll a future without an async context to figure out if it has completed or
540
573
// not without awaiting, we need a Waker, which needs a vtable...we fill it with dummy values
541
574
// but sadly there's a good bit of boilerplate here.
@@ -557,7 +590,7 @@ pub(crate) mod futures_util {
557
590
#[ cfg( feature = "futures" ) ]
558
591
use core:: task;
559
592
#[ cfg( feature = "futures" ) ]
560
- use futures_util:: { dummy_waker, Selector , SelectorOutput } ;
593
+ use futures_util:: { dummy_waker, OptionalSelector , Selector , SelectorOutput } ;
561
594
562
595
/// Processes background events in a future.
563
596
///
@@ -782,18 +815,25 @@ where
782
815
scorer,
783
816
should_break,
784
817
{
818
+ let om_fut = if let Some ( om) = onion_messenger. as_ref( ) {
819
+ let fut = om. get_om( ) . get_update_future( ) ;
820
+ OptionalSelector { optional_future: Some ( fut) }
821
+ } else {
822
+ OptionalSelector { optional_future: None }
823
+ } ;
785
824
let fut = Selector {
786
825
a: channel_manager. get_cm( ) . get_event_or_persistence_needed_future( ) ,
787
826
b: chain_monitor. get_update_future( ) ,
788
- c: sleeper( if mobile_interruptable_platform {
827
+ c: om_fut,
828
+ d: sleeper( if mobile_interruptable_platform {
789
829
Duration :: from_millis( 100 )
790
830
} else {
791
831
Duration :: from_secs( FASTEST_TIMER )
792
832
} ) ,
793
833
} ;
794
834
match fut. await {
795
- SelectorOutput :: A | SelectorOutput :: B => { } ,
796
- SelectorOutput :: C ( exit) => {
835
+ SelectorOutput :: A | SelectorOutput :: B | SelectorOutput :: C => { } ,
836
+ SelectorOutput :: D ( exit) => {
797
837
should_break = exit;
798
838
} ,
799
839
}
@@ -938,11 +978,19 @@ impl BackgroundProcessor {
938
978
scorer,
939
979
stop_thread. load( Ordering :: Acquire ) ,
940
980
{
941
- Sleeper :: from_two_futures(
942
- & channel_manager. get_cm( ) . get_event_or_persistence_needed_future( ) ,
943
- & chain_monitor. get_update_future( ) ,
944
- )
945
- . wait_timeout( Duration :: from_millis( 100 ) ) ;
981
+ let sleeper = if let Some ( om) = onion_messenger. as_ref( ) {
982
+ Sleeper :: from_three_futures(
983
+ & channel_manager. get_cm( ) . get_event_or_persistence_needed_future( ) ,
984
+ & chain_monitor. get_update_future( ) ,
985
+ & om. get_om( ) . get_update_future( ) ,
986
+ )
987
+ } else {
988
+ Sleeper :: from_two_futures(
989
+ & channel_manager. get_cm( ) . get_event_or_persistence_needed_future( ) ,
990
+ & chain_monitor. get_update_future( ) ,
991
+ )
992
+ } ;
993
+ sleeper. wait_timeout( Duration :: from_millis( 100 ) ) ;
946
994
} ,
947
995
|_| Instant :: now( ) ,
948
996
|time: & Instant , dur| time. elapsed( ) . as_secs( ) > dur,
0 commit comments