Skip to content

Commit 54e9b12

Browse files
Send pings in P2PStream.poll_ready (#931)
1 parent 78ffd0a commit 54e9b12

File tree

1 file changed

+25
-25
lines changed

1 file changed

+25
-25
lines changed

crates/net/eth-wire/src/p2pstream.rs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -279,31 +279,6 @@ where
279279
return Poll::Ready(None)
280280
}
281281

282-
// poll the pinger to determine if we should send a ping
283-
match this.pinger.poll_ping(cx) {
284-
Poll::Pending => {}
285-
Poll::Ready(Ok(PingerEvent::Ping)) => {
286-
// encode the ping message
287-
let mut ping_bytes = BytesMut::new();
288-
P2PMessage::Ping.encode(&mut ping_bytes);
289-
290-
// check if the buffer is full
291-
if this.outgoing_messages.len() >= MAX_P2P_CAPACITY {
292-
return Poll::Ready(Some(Err(P2PStreamError::SendBufferFull)))
293-
}
294-
295-
// if the sink is not ready, buffer the message
296-
this.outgoing_messages.push_back(ping_bytes.into());
297-
}
298-
_ => {
299-
// encode the disconnect message
300-
this.start_disconnect(DisconnectReason::PingTimeout)?;
301-
302-
// End the stream after ping related error
303-
return Poll::Ready(None)
304-
}
305-
}
306-
307282
// we should loop here to ensure we don't return Poll::Pending if we have a message to
308283
// return behind any pings we need to respond to
309284
while let Poll::Ready(res) = this.inner.poll_next_unpin(cx) {
@@ -416,6 +391,31 @@ where
416391
fn poll_ready(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
417392
let mut this = self.as_mut();
418393

394+
// poll the pinger to determine if we should send a ping
395+
match this.pinger.poll_ping(cx) {
396+
Poll::Pending => {}
397+
Poll::Ready(Ok(PingerEvent::Ping)) => {
398+
// encode the ping message
399+
let mut ping_bytes = BytesMut::new();
400+
P2PMessage::Ping.encode(&mut ping_bytes);
401+
402+
// check if the buffer is full
403+
if this.outgoing_messages.len() >= MAX_P2P_CAPACITY {
404+
return Poll::Ready(Err(P2PStreamError::SendBufferFull))
405+
}
406+
407+
// if the sink is not ready, buffer the message
408+
this.outgoing_messages.push_back(ping_bytes.into());
409+
}
410+
_ => {
411+
// encode the disconnect message
412+
this.start_disconnect(DisconnectReason::PingTimeout)?;
413+
414+
// End the stream after ping related error
415+
return Poll::Ready(Ok(()))
416+
}
417+
}
418+
419419
match this.inner.poll_ready_unpin(cx) {
420420
Poll::Pending => {}
421421
Poll::Ready(Err(err)) => return Poll::Ready(Err(P2PStreamError::Io(err))),

0 commit comments

Comments
 (0)