@@ -19,7 +19,7 @@ use futures::future::{ready, BoxFuture, Ready};
1919use futures:: sink:: With ;
2020use futures:: stream:: { FuturesUnordered , Map , Stream } ;
2121use futures:: { pin_mut, FutureExt , Sink , SinkExt , StreamExt } ;
22- use libp2p:: gossipsub:: { SubscriptionError , TopicHash } ;
22+ use libp2p:: gossipsub:: { MessageId , PublishError , SubscriptionError , TopicHash } ;
2323use libp2p:: identity:: Keypair ;
2424use libp2p:: metrics:: { Metrics , Recorder , Registry } ;
2525use libp2p:: swarm:: SwarmEvent ;
@@ -72,21 +72,30 @@ impl<SwarmT: SwarmTrait> GenericNetworkManager<SwarmT> {
7272 if let Some ( metrics) = self . metrics . as_ref ( ) {
7373 metrics. register ( ) ;
7474 }
75+
76+ let mut message_to_send: Option < ( Bytes , TopicHash ) > = None ;
7577 loop {
78+ if let Some ( ( message, topic_hash) ) = message_to_send. as_ref ( ) {
79+ match self . broadcast_message ( message. clone ( ) , topic_hash. clone ( ) ) {
80+ Ok ( _) => {
81+ message_to_send = None ;
82+ }
83+ Err ( e) => {
84+ warn ! ( "Failed to broadcast message: `{e:?}` Applying Backpressure." ) ;
85+ }
86+ } ;
87+ }
7688 tokio:: select! {
7789 Some ( event) = self . swarm. next( ) => self . handle_swarm_event( event) ?,
7890 Some ( res) = self . sqmr_inbound_response_receivers. next( ) => self . handle_response_for_inbound_query( res) ,
7991 Some ( ( protocol, client_payload) ) = self . sqmr_outbound_payload_receivers. next( ) => {
8092 let protocol = StreamProtocol :: try_from_owned( protocol) . expect( "Invalid protocol should not appear" ) ;
8193 self . handle_local_sqmr_payload( protocol, client_payload. expect( "An SQMR client channel should not be terminated." ) )
8294 }
83- Some ( ( topic_hash, message) ) = self . messages_to_broadcast_receivers. next( ) => {
84- self . broadcast_message(
85- message. ok_or( NetworkError :: BroadcastChannelsDropped {
86- topic_hash: topic_hash. clone( )
87- } ) ?,
88- topic_hash,
89- ) ;
95+ Some ( ( topic_hash, message) ) = self . messages_to_broadcast_receivers. next( ) , if message_to_send. is_none( ) => {
96+ message_to_send = Some ( ( message. ok_or( NetworkError :: BroadcastChannelsDropped {
97+ topic_hash: topic_hash. clone( ) ,
98+ } ) ?, topic_hash) ) ;
9099 }
91100 Some ( Some ( peer_id) ) = self . reported_peer_receivers. next( ) => self . swarm. report_peer_as_malicious( peer_id, MisconductScore :: MALICIOUS ) ,
92101 Some ( peer_id) = self . reported_peers_receiver. next( ) => self . swarm. report_peer_as_malicious( peer_id, MisconductScore :: MALICIOUS ) ,
@@ -642,7 +651,11 @@ impl<SwarmT: SwarmTrait> GenericNetworkManager<SwarmT> {
642651 . insert ( outbound_session_id, report_receiver) ;
643652 }
644653
645- fn broadcast_message ( & mut self , message : Bytes , topic_hash : TopicHash ) {
654+ fn broadcast_message (
655+ & mut self ,
656+ message : Bytes ,
657+ topic_hash : TopicHash ,
658+ ) -> Result < MessageId , PublishError > {
646659 if let Some ( broadcast_metrics_by_topic) =
647660 self . metrics . as_ref ( ) . and_then ( |metrics| metrics. broadcast_metrics_by_topic . as_ref ( ) )
648661 {
@@ -654,7 +667,7 @@ impl<SwarmT: SwarmTrait> GenericNetworkManager<SwarmT> {
654667 }
655668 }
656669 trace ! ( "Sending broadcast message with topic hash: {topic_hash:?}" ) ;
657- self . swarm . broadcast_message ( message, topic_hash) ;
670+ self . swarm . broadcast_message ( message, topic_hash)
658671 }
659672
660673 fn report_session_removed_to_metrics ( & mut self , session_id : SessionId ) {
0 commit comments