@@ -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,21 +392,27 @@ 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.
401
- fn buffer_full_drop_gossip ( & self ) -> bool {
402
- if self . pending_outbound_buffer . len ( ) > OUTBOUND_BUFFER_LIMIT_DROP_GOSSIP
403
- || self . msgs_sent_since_pong > BUFFER_DRAIN_MSGS_PER_TICK * FORWARD_INIT_SYNC_BUFFER_LIMIT_RATIO {
404
- return false
405
- }
406
- true
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.
410
+ fn buffer_full_drop_gossip_broadcast ( & self ) -> bool {
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 ||
415
+ self . msgs_sent_since_pong > BUFFER_DRAIN_MSGS_PER_TICK * FORWARD_INIT_SYNC_BUFFER_LIMIT_RATIO
407
416
}
408
417
}
409
418
@@ -671,6 +680,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref, CMH: Deref> P
671
680
672
681
pending_outbound_buffer : LinkedList :: new ( ) ,
673
682
pending_outbound_buffer_first_msg_offset : 0 ,
683
+ gossip_broadcast_buffer : LinkedList :: new ( ) ,
674
684
awaiting_write_event : false ,
675
685
676
686
pending_read_buffer,
@@ -717,6 +727,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref, CMH: Deref> P
717
727
718
728
pending_outbound_buffer : LinkedList :: new ( ) ,
719
729
pending_outbound_buffer_first_msg_offset : 0 ,
730
+ gossip_broadcast_buffer : LinkedList :: new ( ) ,
720
731
awaiting_write_event : false ,
721
732
722
733
pending_read_buffer,
@@ -737,6 +748,11 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref, CMH: Deref> P
737
748
738
749
fn do_attempt_write_data ( & self , descriptor : & mut Descriptor , peer : & mut Peer ) {
739
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
+ }
740
756
if peer. should_buffer_gossip_backfill ( ) {
741
757
match peer. sync_status {
742
758
InitSyncTracker :: NoSyncRequested => { } ,
@@ -851,12 +867,6 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref, CMH: Deref> P
851
867
}
852
868
}
853
869
854
- /// Append a message to a peer's pending outbound/write buffer
855
- fn enqueue_encoded_message ( & self , peer : & mut Peer , encoded_message : & Vec < u8 > ) {
856
- peer. msgs_sent_since_pong += 1 ;
857
- peer. pending_outbound_buffer . push_back ( peer. channel_encryptor . encrypt_message ( & encoded_message[ ..] ) ) ;
858
- }
859
-
860
870
/// Append a message to a peer's pending outbound/write buffer
861
871
fn enqueue_message < M : wire:: Type > ( & self , peer : & mut Peer , message : & M ) {
862
872
let mut buffer = VecWriter ( Vec :: with_capacity ( 2048 ) ) ;
@@ -867,7 +877,14 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref, CMH: Deref> P
867
877
} else {
868
878
log_trace ! ( self . logger, "Enqueueing message {:?} to {}" , message, log_pubkey!( peer. their_node_id. unwrap( ) ) )
869
879
}
870
- 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[ ..] ) ) ;
871
888
}
872
889
873
890
fn do_read_event ( & self , peer_descriptor : & mut Descriptor , data : & [ u8 ] ) -> Result < bool , PeerHandleError > {
@@ -1325,7 +1342,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref, CMH: Deref> P
1325
1342
!peer. should_forward_channel_announcement ( msg. contents . short_channel_id ) {
1326
1343
continue
1327
1344
}
1328
- if peer. buffer_full_drop_gossip ( ) {
1345
+ if peer. buffer_full_drop_gossip_broadcast ( ) {
1329
1346
log_gossip ! ( self . logger, "Skipping broadcast message to {:?} as its outbound buffer is full" , peer. their_node_id) ;
1330
1347
continue ;
1331
1348
}
@@ -1336,7 +1353,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref, CMH: Deref> P
1336
1353
if except_node. is_some ( ) && peer. their_node_id . as_ref ( ) == except_node {
1337
1354
continue ;
1338
1355
}
1339
- self . enqueue_encoded_message ( & mut * peer, & encoded_msg) ;
1356
+ self . enqueue_encoded_gossip_broadcast ( & mut * peer, & encoded_msg) ;
1340
1357
}
1341
1358
} ,
1342
1359
wire:: Message :: NodeAnnouncement ( ref msg) => {
@@ -1349,7 +1366,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref, CMH: Deref> P
1349
1366
!peer. should_forward_node_announcement ( msg. contents . node_id ) {
1350
1367
continue
1351
1368
}
1352
- if peer. buffer_full_drop_gossip ( ) {
1369
+ if peer. buffer_full_drop_gossip_broadcast ( ) {
1353
1370
log_gossip ! ( self . logger, "Skipping broadcast message to {:?} as its outbound buffer is full" , peer. their_node_id) ;
1354
1371
continue ;
1355
1372
}
@@ -1359,7 +1376,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref, CMH: Deref> P
1359
1376
if except_node. is_some ( ) && peer. their_node_id . as_ref ( ) == except_node {
1360
1377
continue ;
1361
1378
}
1362
- self . enqueue_encoded_message ( & mut * peer, & encoded_msg) ;
1379
+ self . enqueue_encoded_gossip_broadcast ( & mut * peer, & encoded_msg) ;
1363
1380
}
1364
1381
} ,
1365
1382
wire:: Message :: ChannelUpdate ( ref msg) => {
@@ -1372,14 +1389,14 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref, CMH: Deref> P
1372
1389
!peer. should_forward_channel_announcement ( msg. contents . short_channel_id ) {
1373
1390
continue
1374
1391
}
1375
- if peer. buffer_full_drop_gossip ( ) {
1392
+ if peer. buffer_full_drop_gossip_broadcast ( ) {
1376
1393
log_gossip ! ( self . logger, "Skipping broadcast message to {:?} as its outbound buffer is full" , peer. their_node_id) ;
1377
1394
continue ;
1378
1395
}
1379
1396
if except_node. is_some ( ) && peer. their_node_id . as_ref ( ) == except_node {
1380
1397
continue ;
1381
1398
}
1382
- self . enqueue_encoded_message ( & mut * peer, & encoded_msg) ;
1399
+ self . enqueue_encoded_gossip_broadcast ( & mut * peer, & encoded_msg) ;
1383
1400
}
1384
1401
} ,
1385
1402
_ => debug_assert ! ( false , "We shouldn't attempt to forward anything but gossip messages" ) ,
0 commit comments