Skip to content

Commit 69ba24a

Browse files
committed
f redo max tick interval logic, this much better matches reality
1 parent 63c61d9 commit 69ba24a

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

lightning/src/ln/peer_handler.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -298,23 +298,27 @@ const OUTBOUND_BUFFER_LIMIT_READ_PAUSE: usize = 10;
298298
/// the peer.
299299
const OUTBOUND_BUFFER_LIMIT_DROP_GOSSIP: usize = OUTBOUND_BUFFER_LIMIT_READ_PAUSE * FORWARD_INIT_SYNC_BUFFER_SIZE_LIMIT;
300300

301-
/// If we've sent a ping, and are still awaiting a response, we (or our peer) may need to churn our
302-
/// (or their) way through the socket receive buffer before receiving the ping.
301+
/// If we've sent a ping, and are still awaiting a response, we may need to churn our way through
302+
/// the socket receive buffer before receiving the ping.
303303
///
304304
/// On a fairly old Arm64 board, with Linux defaults, this can take as long as 20 seconds, not
305-
/// including any network delays or outbound traffic.
305+
/// including any network delays, outbound traffic, or the same for messages from other peers.
306306
///
307307
/// Thus, to avoid needlessly disconnecting a peer, we allow a peer to take this many timer ticks
308-
/// to respond to a ping, as long as they send us at least one message during each tick or if we
309-
/// sent a lot of messages, ensuring we aren't actually just disconnected. With a timer tick
310-
/// interval of five seconds, this translates to about 30 seconds.
311-
pub const MAX_BUFFER_DRAIN_TICK_INTERVALS: i8 = 6;
308+
/// per connected peer to respond to a ping, as long as they send us at least one message during
309+
/// each tick, ensuring we aren't actually just disconnected.
310+
/// With a timer tick interval of five seconds, this translates to about 30 seconds per connected
311+
/// peer.
312+
///
313+
/// When we improve parallelism somewhat we should reduce this to e.g. this many timer ticks per
314+
/// two connected peers, assuming most LDK-running systems have at least two cores.
315+
const MAX_BUFFER_DRAIN_TICK_INTERVALS_PER_PEER: i8 = 6;
312316

313317
/// This is the minimum number of messages we expect a peer to be able to handle within one timer
314318
/// tick. Once we have sent this many messages since the last ping, we send a ping right away to
315319
/// ensures we don't just fill up our send buffer and leave the peer with too many messages to
316320
/// process before the next ping.
317-
pub const BUFFER_DRAIN_MSGS_PER_TICK: usize = 32;
321+
const BUFFER_DRAIN_MSGS_PER_TICK: usize = 32;
318322

319323
struct Peer {
320324
channel_encryptor: PeerChannelEncryptor,
@@ -1523,6 +1527,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref, CMH: Deref> P
15231527
let node_id_to_descriptor = &mut peers.node_id_to_descriptor;
15241528
let peers = &mut peers.peers;
15251529
let mut descriptors_needing_disconnect = Vec::new();
1530+
let peer_count = peers.len();
15261531

15271532
peers.retain(|descriptor, peer| {
15281533
if !peer.channel_encryptor.is_ready_for_encryption() {
@@ -1531,7 +1536,8 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref, CMH: Deref> P
15311536
}
15321537

15331538
if (peer.awaiting_pong_tick_intervals > 0 && !peer.received_message_since_timer_tick)
1534-
|| peer.awaiting_pong_tick_intervals > MAX_BUFFER_DRAIN_TICK_INTERVALS
1539+
|| peer.awaiting_pong_tick_intervals as u64 >
1540+
MAX_BUFFER_DRAIN_TICK_INTERVALS_PER_PEER as u64 * peer_count as u64
15351541
{
15361542
descriptors_needing_disconnect.push(descriptor.clone());
15371543
match peer.their_node_id {

0 commit comments

Comments
 (0)