@@ -337,6 +337,9 @@ struct Peer {
337
337
338
338
pending_outbound_buffer : LinkedList < Vec < u8 > > ,
339
339
pending_outbound_buffer_first_msg_offset : usize ,
340
+ // Queue gossip broadcasts separately from `pending_outbound_buffer` so we can easily prioritize
341
+ // channel messages over them.
342
+ gossip_broadcast_buffer : LinkedList < Vec < u8 > > ,
340
343
awaiting_write_event : bool ,
341
344
342
345
pending_read_buffer : Vec < u8 > ,
@@ -389,17 +392,26 @@ impl Peer {
389
392
self . pending_outbound_buffer . len ( ) < OUTBOUND_BUFFER_LIMIT_READ_PAUSE
390
393
}
391
394
392
- /// Determines if we should push additional gossip messages onto a peer's outbound buffer for
393
- /// backfilling gossip data to the peer. This is checked every time the peer's buffer may have
394
- /// been drained.
395
+ /// Determines if we should push additional gossip background sync (aka "backfill") onto a peer's
396
+ /// outbound buffer. This is checked every time the peer's buffer may have been drained.
395
397
fn should_buffer_gossip_backfill ( & self ) -> bool {
396
- self . pending_outbound_buffer . is_empty ( ) &&
397
- self . msgs_sent_since_pong < BUFFER_DRAIN_MSGS_PER_TICK
398
+ self . pending_outbound_buffer . is_empty ( ) && self . gossip_broadcast_buffer . is_empty ( )
399
+ && self . msgs_sent_since_pong < BUFFER_DRAIN_MSGS_PER_TICK
398
400
}
399
401
400
- /// Returns whether this peer's buffer is full and we should drop gossip messages.
402
+ /// Determines if we should push additional gossip broadcast messages onto a peer's outbound
403
+ /// buffer. This is checked every time the peer's buffer may have been drained.
404
+ fn should_buffer_gossip_broadcast ( & self ) -> bool {
405
+ self . pending_outbound_buffer . is_empty ( )
406
+ && self . msgs_sent_since_pong < BUFFER_DRAIN_MSGS_PER_TICK
407
+ }
408
+
409
+ /// Returns whether this peer's outbound buffers are full and we should drop gossip broadcasts.
401
410
fn buffer_full_drop_gossip_broadcast ( & self ) -> bool {
402
- self . pending_outbound_buffer . len ( ) > OUTBOUND_BUFFER_LIMIT_DROP_GOSSIP ||
411
+ let total_outbound_buffered =
412
+ self . gossip_broadcast_buffer . len ( ) + self . pending_outbound_buffer . len ( ) ;
413
+
414
+ total_outbound_buffered > OUTBOUND_BUFFER_LIMIT_DROP_GOSSIP ||
403
415
self . msgs_sent_since_pong > BUFFER_DRAIN_MSGS_PER_TICK * FORWARD_INIT_SYNC_BUFFER_LIMIT_RATIO
404
416
}
405
417
}
@@ -668,6 +680,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref, CMH: Deref> P
668
680
669
681
pending_outbound_buffer : LinkedList :: new ( ) ,
670
682
pending_outbound_buffer_first_msg_offset : 0 ,
683
+ gossip_broadcast_buffer : LinkedList :: new ( ) ,
671
684
awaiting_write_event : false ,
672
685
673
686
pending_read_buffer,
@@ -714,6 +727,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref, CMH: Deref> P
714
727
715
728
pending_outbound_buffer : LinkedList :: new ( ) ,
716
729
pending_outbound_buffer_first_msg_offset : 0 ,
730
+ gossip_broadcast_buffer : LinkedList :: new ( ) ,
717
731
awaiting_write_event : false ,
718
732
719
733
pending_read_buffer,
@@ -734,6 +748,11 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref, CMH: Deref> P
734
748
735
749
fn do_attempt_write_data ( & self , descriptor : & mut Descriptor , peer : & mut Peer ) {
736
750
while !peer. awaiting_write_event {
751
+ if peer. should_buffer_gossip_broadcast ( ) {
752
+ if let Some ( msg) = peer. gossip_broadcast_buffer . pop_front ( ) {
753
+ peer. pending_outbound_buffer . push_back ( msg) ;
754
+ }
755
+ }
737
756
if peer. should_buffer_gossip_backfill ( ) {
738
757
match peer. sync_status {
739
758
InitSyncTracker :: NoSyncRequested => { } ,
@@ -848,12 +867,6 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref, CMH: Deref> P
848
867
}
849
868
}
850
869
851
- /// Append a message to a peer's pending outbound/write buffer
852
- fn enqueue_encoded_message ( & self , peer : & mut Peer , encoded_message : & Vec < u8 > ) {
853
- peer. msgs_sent_since_pong += 1 ;
854
- peer. pending_outbound_buffer . push_back ( peer. channel_encryptor . encrypt_message ( & encoded_message[ ..] ) ) ;
855
- }
856
-
857
870
/// Append a message to a peer's pending outbound/write buffer
858
871
fn enqueue_message < M : wire:: Type > ( & self , peer : & mut Peer , message : & M ) {
859
872
let mut buffer = VecWriter ( Vec :: with_capacity ( 2048 ) ) ;
@@ -864,7 +877,14 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref, CMH: Deref> P
864
877
} else {
865
878
log_trace ! ( self . logger, "Enqueueing message {:?} to {}" , message, log_pubkey!( peer. their_node_id. unwrap( ) ) )
866
879
}
867
- self . enqueue_encoded_message ( peer, & buffer. 0 ) ;
880
+ peer. msgs_sent_since_pong += 1 ;
881
+ peer. pending_outbound_buffer . push_back ( peer. channel_encryptor . encrypt_message ( & buffer. 0 [ ..] ) ) ;
882
+ }
883
+
884
+ /// Append a message to a peer's pending outbound/write gossip broadcast buffer
885
+ fn enqueue_encoded_gossip_broadcast ( & self , peer : & mut Peer , encoded_message : & Vec < u8 > ) {
886
+ peer. msgs_sent_since_pong += 1 ;
887
+ peer. gossip_broadcast_buffer . push_back ( peer. channel_encryptor . encrypt_message ( & encoded_message[ ..] ) ) ;
868
888
}
869
889
870
890
fn do_read_event ( & self , peer_descriptor : & mut Descriptor , data : & [ u8 ] ) -> Result < bool , PeerHandleError > {
@@ -1333,7 +1353,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref, CMH: Deref> P
1333
1353
if except_node. is_some ( ) && peer. their_node_id . as_ref ( ) == except_node {
1334
1354
continue ;
1335
1355
}
1336
- self . enqueue_encoded_message ( & mut * peer, & encoded_msg) ;
1356
+ self . enqueue_encoded_gossip_broadcast ( & mut * peer, & encoded_msg) ;
1337
1357
}
1338
1358
} ,
1339
1359
wire:: Message :: NodeAnnouncement ( ref msg) => {
@@ -1356,7 +1376,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref, CMH: Deref> P
1356
1376
if except_node. is_some ( ) && peer. their_node_id . as_ref ( ) == except_node {
1357
1377
continue ;
1358
1378
}
1359
- self . enqueue_encoded_message ( & mut * peer, & encoded_msg) ;
1379
+ self . enqueue_encoded_gossip_broadcast ( & mut * peer, & encoded_msg) ;
1360
1380
}
1361
1381
} ,
1362
1382
wire:: Message :: ChannelUpdate ( ref msg) => {
@@ -1376,7 +1396,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref, CMH: Deref> P
1376
1396
if except_node. is_some ( ) && peer. their_node_id . as_ref ( ) == except_node {
1377
1397
continue ;
1378
1398
}
1379
- self . enqueue_encoded_message ( & mut * peer, & encoded_msg) ;
1399
+ self . enqueue_encoded_gossip_broadcast ( & mut * peer, & encoded_msg) ;
1380
1400
}
1381
1401
} ,
1382
1402
_ => debug_assert ! ( false , "We shouldn't attempt to forward anything but gossip messages" ) ,
0 commit comments