@@ -18,7 +18,7 @@ use futures::future::{ready, BoxFuture, Ready};
1818use futures:: sink:: With ;
1919use futures:: stream:: { FuturesUnordered , Map , Stream } ;
2020use futures:: { pin_mut, FutureExt , Sink , SinkExt , StreamExt } ;
21- use libp2p:: gossipsub:: { SubscriptionError , TopicHash } ;
21+ use libp2p:: gossipsub:: { MessageId , PublishError , SubscriptionError , TopicHash } ;
2222use libp2p:: identity:: Keypair ;
2323use libp2p:: swarm:: SwarmEvent ;
2424use libp2p:: { noise, yamux, Multiaddr , PeerId , StreamProtocol , Swarm , SwarmBuilder } ;
@@ -68,21 +68,30 @@ impl<SwarmT: SwarmTrait> GenericNetworkManager<SwarmT> {
6868 if let Some ( metrics) = self . metrics . as_ref ( ) {
6969 metrics. register ( ) ;
7070 }
71+
72+ let mut message_to_send: Option < ( Bytes , TopicHash ) > = None ;
7173 loop {
74+ if let Some ( ( message, topic_hash) ) = message_to_send. as_ref ( ) {
75+ match self . broadcast_message ( message. clone ( ) , topic_hash. clone ( ) ) {
76+ Ok ( _) => {
77+ message_to_send = None ;
78+ }
79+ Err ( e) => {
80+ warn ! ( "Failed to broadcast message: `{e:?}` Applying Backpressure." ) ;
81+ }
82+ } ;
83+ }
7284 tokio:: select! {
7385 Some ( event) = self . swarm. next( ) => self . handle_swarm_event( event) ?,
7486 Some ( res) = self . sqmr_inbound_response_receivers. next( ) => self . handle_response_for_inbound_query( res) ,
7587 Some ( ( protocol, client_payload) ) = self . sqmr_outbound_payload_receivers. next( ) => {
7688 let protocol = StreamProtocol :: try_from_owned( protocol) . expect( "Invalid protocol should not appear" ) ;
7789 self . handle_local_sqmr_payload( protocol, client_payload. expect( "An SQMR client channel should not be terminated." ) )
7890 }
79- Some ( ( topic_hash, message) ) = self . messages_to_broadcast_receivers. next( ) => {
80- self . broadcast_message(
81- message. ok_or( NetworkError :: BroadcastChannelsDropped {
82- topic_hash: topic_hash. clone( )
83- } ) ?,
84- topic_hash,
85- ) ;
91+ Some ( ( topic_hash, message) ) = self . messages_to_broadcast_receivers. next( ) , if message_to_send. is_none( ) => {
92+ message_to_send = Some ( ( message. ok_or( NetworkError :: BroadcastChannelsDropped {
93+ topic_hash: topic_hash. clone( ) ,
94+ } ) ?, topic_hash) ) ;
8695 }
8796 Some ( Some ( peer_id) ) = self . reported_peer_receivers. next( ) => self . swarm. report_peer_as_malicious( peer_id, MisconductScore :: MALICIOUS ) ,
8897 Some ( peer_id) = self . reported_peers_receiver. next( ) => self . swarm. report_peer_as_malicious( peer_id, MisconductScore :: MALICIOUS ) ,
@@ -615,7 +624,11 @@ impl<SwarmT: SwarmTrait> GenericNetworkManager<SwarmT> {
615624 . insert ( outbound_session_id, report_receiver) ;
616625 }
617626
618- fn broadcast_message ( & mut self , message : Bytes , topic_hash : TopicHash ) {
627+ fn broadcast_message (
628+ & mut self ,
629+ message : Bytes ,
630+ topic_hash : TopicHash ,
631+ ) -> Result < MessageId , PublishError > {
619632 if let Some ( broadcast_metrics_by_topic) =
620633 self . metrics . as_ref ( ) . and_then ( |metrics| metrics. broadcast_metrics_by_topic . as_ref ( ) )
621634 {
@@ -627,7 +640,7 @@ impl<SwarmT: SwarmTrait> GenericNetworkManager<SwarmT> {
627640 }
628641 }
629642 trace ! ( "Sending broadcast message with topic hash: {topic_hash:?}" ) ;
630- self . swarm . broadcast_message ( message, topic_hash) ;
643+ self . swarm . broadcast_message ( message, topic_hash)
631644 }
632645
633646 fn report_session_removed_to_metrics ( & mut self , session_id : SessionId ) {
0 commit comments