@@ -910,30 +910,51 @@ impl Node {
910
910
let bcast_cm = Arc :: clone ( & self . channel_manager ) ;
911
911
let bcast_pm = Arc :: clone ( & self . peer_manager ) ;
912
912
let bcast_config = Arc :: clone ( & self . config ) ;
913
+ let bcast_store = Arc :: clone ( & self . kv_store ) ;
914
+ let bcast_logger = Arc :: clone ( & self . logger ) ;
913
915
let mut stop_bcast = self . stop_receiver . clone ( ) ;
914
916
runtime. spawn ( async move {
915
- let mut interval = tokio:: time:: interval ( NODE_ANN_BCAST_INTERVAL ) ;
917
+ // We check every 30 secs whether our last broadcast is NODE_ANN_BCAST_INTERVAL away.
918
+ let mut interval = tokio:: time:: interval ( Duration :: from_secs ( 30 ) ) ;
916
919
loop {
917
920
tokio:: select! {
918
921
_ = stop_bcast. changed( ) => {
919
922
return ;
920
923
}
921
- _ = interval. tick( ) , if bcast_cm . list_channels ( ) . iter ( ) . any ( |chan| chan . is_public ) => {
922
- while bcast_pm . get_peer_node_ids ( ) . is_empty ( ) {
923
- // Sleep a bit and retry if we don't have any peers yet.
924
- tokio :: time:: sleep ( Duration :: from_secs ( 5 ) ) . await ;
925
-
926
- // Check back if we need to stop.
927
- match stop_bcast . has_changed ( ) {
928
- Ok ( false ) => { } ,
929
- Ok ( true ) => return ,
930
- Err ( _ ) => return ,
924
+ _ = interval. tick( ) => {
925
+ let skip_broadcast = match io :: utils :: read_latest_node_ann_bcast_timestamp ( Arc :: clone ( & bcast_store ) ) {
926
+ Ok ( latest_bcast_time_secs ) => {
927
+ // Skip if the time hasn't elapsed yet.
928
+ let next_bcast_unix_time = SystemTime :: UNIX_EPOCH + Duration :: from_secs ( latest_bcast_time_secs ) + NODE_ANN_BCAST_INTERVAL ;
929
+ next_bcast_unix_time . elapsed ( ) . is_err ( )
930
+ }
931
+ Err ( _ ) => {
932
+ // Don't skip if we haven't broadcasted before.
933
+ false
931
934
}
935
+ } ;
936
+
937
+ if skip_broadcast {
938
+ continue ;
939
+ }
940
+
941
+ if bcast_cm. list_channels( ) . iter( ) . any( |chan| chan. is_public) {
942
+ // Skip if we don't have any public channels.
943
+ continue ;
944
+ }
945
+
946
+ if bcast_pm. get_peer_node_ids( ) . is_empty( ) {
947
+ // Skip if we don't have any connected peers to gossip to.
948
+ continue ;
932
949
}
933
950
934
951
let addresses =
935
952
bcast_config. listening_address. iter( ) . cloned( ) . map( |a| a. 0 ) . collect( ) ;
936
953
bcast_pm. broadcast_node_announcement( [ 0 ; 3 ] , [ 0 ; 32 ] , addresses) ;
954
+
955
+ let unix_time_secs = SystemTime :: now( ) . duration_since( SystemTime :: UNIX_EPOCH ) . unwrap( ) . as_secs( ) ;
956
+ io:: utils:: write_latest_node_ann_bcast_timestamp( unix_time_secs, Arc :: clone( & bcast_store) , Arc :: clone( & bcast_logger) )
957
+ . expect( "Persistence failed" ) ;
937
958
}
938
959
}
939
960
}
0 commit comments