@@ -45,8 +45,6 @@ use libp2p_swarm::{
45
45
THandlerOutEvent , ToSwarm ,
46
46
} ;
47
47
48
- use crate :: backoff:: BackoffStorage ;
49
- use crate :: config:: { Config , ValidationMode } ;
50
48
use crate :: gossip_promises:: GossipPromises ;
51
49
use crate :: handler:: { Handler , HandlerEvent , HandlerIn } ;
52
50
use crate :: mcache:: MessageCache ;
@@ -62,6 +60,11 @@ use crate::types::{
62
60
SubscriptionAction ,
63
61
} ;
64
62
use crate :: types:: { PeerConnections , PeerKind , RpcOut } ;
63
+ use crate :: { backoff:: BackoffStorage , types:: RpcSender } ;
64
+ use crate :: {
65
+ config:: { Config , ValidationMode } ,
66
+ types:: rpc_channel,
67
+ } ;
65
68
use crate :: { rpc_proto:: proto, TopicScoreParams } ;
66
69
use crate :: { PublishError , SubscriptionError , ValidationError } ;
67
70
use instant:: SystemTime ;
@@ -332,6 +335,9 @@ pub struct Behaviour<D = IdentityTransform, F = AllowAllSubscriptionFilter> {
332
335
333
336
/// Keep track of a set of internal metrics relating to gossipsub.
334
337
metrics : Option < Metrics > ,
338
+
339
+ /// Connection handler message queue channels.
340
+ handler_send_queues : HashMap < PeerId , RpcSender > ,
335
341
}
336
342
337
343
impl < D , F > Behaviour < D , F >
@@ -471,6 +477,7 @@ where
471
477
config,
472
478
subscription_filter,
473
479
data_transform,
480
+ handler_send_queues : Default :: default ( ) ,
474
481
} )
475
482
}
476
483
}
@@ -537,7 +544,8 @@ where
537
544
for peer in self . peer_topics . keys ( ) . copied ( ) . collect :: < Vec < _ > > ( ) {
538
545
tracing:: debug!( %peer, "Sending SUBSCRIBE to peer" ) ;
539
546
let event = RpcOut :: Subscribe ( topic_hash. clone ( ) ) ;
540
- self . send_message ( peer, event) ;
547
+ self . send_message ( peer, event)
548
+ . expect ( "Subscribe messages should be always sent" ) ;
541
549
}
542
550
543
551
// call JOIN(topic)
@@ -564,7 +572,8 @@ where
564
572
for peer in self . peer_topics . keys ( ) . copied ( ) . collect :: < Vec < _ > > ( ) {
565
573
tracing:: debug!( %peer, "Sending UNSUBSCRIBE to peer" ) ;
566
574
let event = RpcOut :: Unsubscribe ( topic_hash. clone ( ) ) ;
567
- self . send_message ( peer, event) ;
575
+ self . send_message ( peer, event)
576
+ . expect ( "Subscribe messages should be always sent" ) ;
568
577
}
569
578
570
579
// call LEAVE(topic)
@@ -711,9 +720,18 @@ where
711
720
}
712
721
713
722
// Send to peers we know are subscribed to the topic.
723
+ let mut errors = 0 ;
714
724
for peer_id in recipient_peers. iter ( ) {
715
725
tracing:: trace!( peer=%peer_id, "Sending message to peer" ) ;
716
- self . send_message ( * peer_id, RpcOut :: Publish ( raw_message. clone ( ) ) ) ;
726
+ if self
727
+ . send_message ( * peer_id, RpcOut :: Publish ( raw_message. clone ( ) ) )
728
+ . is_err ( )
729
+ {
730
+ errors += 1 ;
731
+ }
732
+ }
733
+ if errors == recipient_peers. len ( ) {
734
+ return Err ( PublishError :: InsufficientPeers ) ;
717
735
}
718
736
719
737
tracing:: debug!( message=%msg_id, "Published message" ) ;
@@ -1311,7 +1329,7 @@ where
1311
1329
) ;
1312
1330
} else {
1313
1331
tracing:: debug!( peer=%peer_id, "IWANT: Sending cached messages to peer" ) ;
1314
- self . send_message ( * peer_id, RpcOut :: Forward ( msg) ) ;
1332
+ let _ = self . send_message ( * peer_id, RpcOut :: Forward ( msg) ) ;
1315
1333
}
1316
1334
}
1317
1335
}
@@ -1469,7 +1487,8 @@ where
1469
1487
. map ( |t| self . make_prune ( t, peer_id, do_px, on_unsubscribe) )
1470
1488
. collect :: < Vec < _ > > ( )
1471
1489
{
1472
- self . send_message ( * peer_id, RpcOut :: Control ( action) ) ;
1490
+ self . send_message ( * peer_id, RpcOut :: Control ( action) )
1491
+ . expect ( "PRUNE messages should always be sent" ) ;
1473
1492
}
1474
1493
// Send the prune messages to the peer
1475
1494
tracing:: debug!(
@@ -1970,6 +1989,7 @@ where
1970
1989
. collect :: < Vec < _ > > ( )
1971
1990
{
1972
1991
self . send_message ( * propagation_source, RpcOut :: Control ( action) )
1992
+ . expect ( "GRAFT messages should always be sent" ) ;
1973
1993
}
1974
1994
1975
1995
// Notify the application of the subscriptions
@@ -2520,7 +2540,8 @@ where
2520
2540
2521
2541
// send the control messages
2522
2542
for msg in control_msgs. chain ( prunes) . collect :: < Vec < _ > > ( ) {
2523
- self . send_message ( peer, RpcOut :: Control ( msg) ) ;
2543
+ self . send_message ( peer, RpcOut :: Control ( msg) )
2544
+ . expect ( "PRUNE messages should always be sent" ) ;
2524
2545
}
2525
2546
}
2526
2547
@@ -2534,7 +2555,8 @@ where
2534
2555
self . config . do_px ( ) && !no_px. contains ( peer) ,
2535
2556
false ,
2536
2557
) ;
2537
- self . send_message ( * peer, RpcOut :: Control ( prune) ) ;
2558
+ self . send_message ( * peer, RpcOut :: Control ( prune) )
2559
+ . expect ( "PRUNE messages should always be sent" ) ;
2538
2560
2539
2561
// inform the handler
2540
2562
peer_removed_from_mesh (
@@ -2606,7 +2628,7 @@ where
2606
2628
2607
2629
for peer in recipient_peers. iter ( ) {
2608
2630
tracing:: debug!( %peer, message=%msg_id, "Sending message to peer" ) ;
2609
- self . send_message ( * peer, event. clone ( ) ) ;
2631
+ let _ = self . send_message ( * peer, event. clone ( ) ) ;
2610
2632
}
2611
2633
tracing:: debug!( "Completed forwarding message" ) ;
2612
2634
Ok ( true )
@@ -2720,7 +2742,7 @@ where
2720
2742
fn flush_control_pool ( & mut self ) {
2721
2743
for ( peer, controls) in self . control_pool . drain ( ) . collect :: < Vec < _ > > ( ) {
2722
2744
for msg in controls {
2723
- self . send_message ( peer, RpcOut :: Control ( msg) ) ;
2745
+ let _ = self . send_message ( peer, RpcOut :: Control ( msg) ) ;
2724
2746
}
2725
2747
}
2726
2748
@@ -2730,19 +2752,24 @@ where
2730
2752
2731
2753
/// Send a [`RpcOut`] message to a peer. This will wrap the message in an arc if it
2732
2754
/// is not already an arc.
2733
- fn send_message ( & mut self , peer_id : PeerId , rpc : RpcOut ) {
2755
+ fn send_message ( & mut self , peer_id : PeerId , rpc : RpcOut ) -> Result < ( ) , ( ) > {
2756
+ let sender = self
2757
+ . handler_send_queues
2758
+ . get_mut ( & peer_id)
2759
+ . expect ( "Peerid should exist" ) ;
2760
+
2761
+ if sender. try_send ( rpc. clone ( ) ) . is_err ( ) {
2762
+ tracing:: debug!( peer=%peer_id, "Dropping message as peer is full" ) ;
2763
+ return Err ( ( ) ) ;
2764
+ }
2765
+
2734
2766
if let Some ( m) = self . metrics . as_mut ( ) {
2735
2767
if let RpcOut :: Publish ( ref message) | RpcOut :: Forward ( ref message) = rpc {
2736
2768
// register bytes sent on the internal metrics.
2737
2769
m. msg_sent ( & message. topic , message. raw_protobuf_len ( ) ) ;
2738
2770
}
2739
2771
}
2740
-
2741
- self . events . push_back ( ToSwarm :: NotifyHandler {
2742
- peer_id,
2743
- event : HandlerIn :: Message ( rpc) ,
2744
- handler : NotifyHandler :: Any ,
2745
- } ) ;
2772
+ Ok ( ( ) )
2746
2773
}
2747
2774
2748
2775
fn on_connection_established (
@@ -2811,7 +2838,8 @@ where
2811
2838
tracing:: debug!( peer=%peer_id, "New peer connected" ) ;
2812
2839
// We need to send our subscriptions to the newly-connected node.
2813
2840
for topic_hash in self . mesh . clone ( ) . into_keys ( ) {
2814
- self . send_message ( peer_id, RpcOut :: Subscribe ( topic_hash) ) ;
2841
+ self . send_message ( peer_id, RpcOut :: Subscribe ( topic_hash) )
2842
+ . expect ( "Subscribe messages should be always sent" ) ;
2815
2843
}
2816
2844
}
2817
2845
@@ -2939,6 +2967,7 @@ where
2939
2967
}
2940
2968
2941
2969
self . connected_peers . remove ( & peer_id) ;
2970
+ self . handler_send_queues . remove ( & peer_id) ;
2942
2971
2943
2972
if let Some ( ( peer_score, ..) ) = & mut self . peer_score {
2944
2973
peer_score. remove_peer ( & peer_id) ;
@@ -2998,21 +3027,25 @@ where
2998
3027
fn handle_established_inbound_connection (
2999
3028
& mut self ,
3000
3029
_: ConnectionId ,
3001
- _ : PeerId ,
3030
+ peer_id : PeerId ,
3002
3031
_: & Multiaddr ,
3003
3032
_: & Multiaddr ,
3004
3033
) -> Result < THandler < Self > , ConnectionDenied > {
3005
- Ok ( Handler :: new ( self . config . protocol_config ( ) ) )
3034
+ let ( sender, receiver) = rpc_channel ( self . config . connection_handler_queue_len ( ) ) ;
3035
+ self . handler_send_queues . insert ( peer_id, sender) ;
3036
+ Ok ( Handler :: new ( self . config . protocol_config ( ) , receiver) )
3006
3037
}
3007
3038
3008
3039
fn handle_established_outbound_connection (
3009
3040
& mut self ,
3010
3041
_: ConnectionId ,
3011
- _ : PeerId ,
3042
+ peer_id : PeerId ,
3012
3043
_: & Multiaddr ,
3013
3044
_: Endpoint ,
3014
3045
) -> Result < THandler < Self > , ConnectionDenied > {
3015
- Ok ( Handler :: new ( self . config . protocol_config ( ) ) )
3046
+ let ( sender, receiver) = rpc_channel ( self . config . connection_handler_queue_len ( ) ) ;
3047
+ self . handler_send_queues . insert ( peer_id, sender) ;
3048
+ Ok ( Handler :: new ( self . config . protocol_config ( ) , receiver) )
3016
3049
}
3017
3050
3018
3051
fn on_connection_handler_event (
0 commit comments