Skip to content

Commit 3d64059

Browse files
committed
Give peers which are sending us messages longer to respond to ping
See comment for rationale.
1 parent 039f05b commit 3d64059

File tree

1 file changed

+32
-10
lines changed

1 file changed

+32
-10
lines changed

lightning/src/ln/peer_handler.rs

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,18 @@ const OUTBOUND_BUFFER_LIMIT_READ_PAUSE: usize = 10;
342342
/// the peer.
343343
const OUTBOUND_BUFFER_LIMIT_DROP_GOSSIP: usize = 20;
344344

345+
/// If we've sent a ping, and are still awaiting a response, we may need to churn our way through
346+
/// the socket receive buffer before we get it.
347+
///
348+
/// On a fairly old Arm64 board, with Linux defaults, this can take as long as 20 seconds, not
349+
/// including any network delays or outbound traffic.
350+
///
351+
/// Thus, to avoid needlessly disconnecting a peer, we allow a peer to take this many timer ticks
352+
/// to respond to a ping, as long as they send us at least one message during each tick, ensuring
353+
/// we aren't actually just disconnected. With a timer tick interval of five seconds, this
354+
/// translates to about 30 seconds.
355+
const MAX_BUFFER_DRAIN_TICK_INTERVALS: u8 = 6;
356+
345357
struct Peer {
346358
channel_encryptor: PeerChannelEncryptor,
347359
their_node_id: Option<PublicKey>,
@@ -357,7 +369,8 @@ struct Peer {
357369

358370
sync_status: InitSyncTracker,
359371

360-
awaiting_pong: bool,
372+
awaiting_pong_tick_intervals: u8,
373+
received_message_since_timer_tick: bool,
361374
}
362375

363376
impl Peer {
@@ -617,7 +630,8 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref, CMH: Deref> P
617630

618631
sync_status: InitSyncTracker::NoSyncRequested,
619632

620-
awaiting_pong: false,
633+
awaiting_pong_tick_intervals: 0,
634+
received_message_since_timer_tick: false,
621635
})).is_some() {
622636
panic!("PeerManager driver duplicated descriptors!");
623637
};
@@ -655,7 +669,8 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref, CMH: Deref> P
655669

656670
sync_status: InitSyncTracker::NoSyncRequested,
657671

658-
awaiting_pong: false,
672+
awaiting_pong_tick_intervals: 0,
673+
received_message_since_timer_tick: false,
659674
})).is_some() {
660675
panic!("PeerManager driver duplicated descriptors!");
661676
};
@@ -996,6 +1011,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref, CMH: Deref> P
9961011
) -> Result<Option<wire::Message<<<CMH as core::ops::Deref>::Target as wire::CustomMessageReader>::CustomMessage>>, MessageHandlingError> {
9971012

9981013
let their_node_id = peer_lock.their_node_id.clone().expect("We know the peer's public key by the time we recieve messages");
1014+
peer_lock.received_message_since_timer_tick = true;
9991015

10001016
// Need an Init as first message
10011017
if let wire::Message::Init(msg) = message {
@@ -1067,7 +1083,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref, CMH: Deref> P
10671083
}
10681084
},
10691085
wire::Message::Pong(_msg) => {
1070-
peer_mutex.lock().unwrap().awaiting_pong = false;
1086+
peer_mutex.lock().unwrap().awaiting_pong_tick_intervals = 0;
10711087
},
10721088

10731089
// Channel messages:
@@ -1581,25 +1597,31 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref, CMH: Deref> P
15811597

15821598
for (descriptor, peer_mutex) in peers_lock.peers.iter() {
15831599
let mut peer = peer_mutex.lock().unwrap();
1584-
if peer.awaiting_pong {
1600+
if !peer.channel_encryptor.is_ready_for_encryption() {
1601+
// The peer needs to complete its handshake before we can exchange messages
1602+
continue;
1603+
}
1604+
1605+
if (peer.awaiting_pong_tick_intervals > 0 && !peer.received_message_since_timer_tick)
1606+
|| peer.awaiting_pong_tick_intervals > MAX_BUFFER_DRAIN_TICK_INTERVALS
1607+
{
15851608
descriptors_needing_disconnect.push(descriptor.clone());
15861609
continue;
15871610
}
15881611

1589-
if !peer.channel_encryptor.is_ready_for_encryption() {
1590-
// The peer needs to complete its handshake before we can exchange messages
1612+
peer.received_message_since_timer_tick = false;
1613+
if peer.awaiting_pong_tick_intervals > 0 {
1614+
peer.awaiting_pong_tick_intervals += 1;
15911615
continue;
15921616
}
1617+
peer.awaiting_pong_tick_intervals = 0;
15931618

15941619
let ping = msgs::Ping {
15951620
ponglen: 0,
15961621
byteslen: 64,
15971622
};
15981623
self.enqueue_message(&mut *peer, &ping);
1599-
16001624
self.do_attempt_write_data(&mut (descriptor.clone()), &mut *peer);
1601-
1602-
peer.awaiting_pong = true;
16031625
}
16041626
}
16051627

0 commit comments

Comments
 (0)