Skip to content

Commit

Permalink
feat(gossipsub): process send-queue before inbound stream
Browse files Browse the repository at this point in the history
Process outbound stream before inbound stream in  `gossipsub::EnabledHandler::poll(..)`.

Pull-Request: #4778.
  • Loading branch information
0xcrust authored Nov 14, 2023
1 parent 69e62cb commit 22f1e23
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 67 deletions.
6 changes: 3 additions & 3 deletions protocols/gossipsub/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
- Remove deprecated `gossipsub::Config::idle_timeout` in favor of `SwarmBuilder::idle_connection_timeout`.
See [PR 4642](https://github.com/libp2p/rust-libp2p/pull/4642).
- Return typed error from config builder.
See [PR 4445].

[PR 4445]: https://github.com/libp2p/rust-libp2p/pull/4445
See [PR 4445](https://github.com/libp2p/rust-libp2p/pull/4445).
- Process outbound stream before inbound stream in `EnabledHandler::poll(..)`.
See [PR 4778](https://github.com/libp2p/rust-libp2p/pull/4778).

## 0.45.2

Expand Down
128 changes: 64 additions & 64 deletions protocols/gossipsub/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,70 +241,6 @@ impl EnabledHandler {
});
}

loop {
match std::mem::replace(
&mut self.inbound_substream,
Some(InboundSubstreamState::Poisoned),
) {
// inbound idle state
Some(InboundSubstreamState::WaitingInput(mut substream)) => {
match substream.poll_next_unpin(cx) {
Poll::Ready(Some(Ok(message))) => {
self.last_io_activity = Instant::now();
self.inbound_substream =
Some(InboundSubstreamState::WaitingInput(substream));
return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour(message));
}
Poll::Ready(Some(Err(error))) => {
tracing::debug!("Failed to read from inbound stream: {error}");
// Close this side of the stream. If the
// peer is still around, they will re-establish their
// outbound stream i.e. our inbound stream.
self.inbound_substream =
Some(InboundSubstreamState::Closing(substream));
}
// peer closed the stream
Poll::Ready(None) => {
tracing::debug!("Inbound stream closed by remote");
self.inbound_substream =
Some(InboundSubstreamState::Closing(substream));
}
Poll::Pending => {
self.inbound_substream =
Some(InboundSubstreamState::WaitingInput(substream));
break;
}
}
}
Some(InboundSubstreamState::Closing(mut substream)) => {
match Sink::poll_close(Pin::new(&mut substream), cx) {
Poll::Ready(res) => {
if let Err(e) = res {
// Don't close the connection but just drop the inbound substream.
// In case the remote has more to send, they will open up a new
// substream.
tracing::debug!("Inbound substream error while closing: {e}");
}
self.inbound_substream = None;
break;
}
Poll::Pending => {
self.inbound_substream =
Some(InboundSubstreamState::Closing(substream));
break;
}
}
}
None => {
self.inbound_substream = None;
break;
}
Some(InboundSubstreamState::Poisoned) => {
unreachable!("Error occurred during inbound stream processing")
}
}
}

// process outbound stream
loop {
match std::mem::replace(
Expand Down Expand Up @@ -382,6 +318,70 @@ impl EnabledHandler {
}
}

loop {
match std::mem::replace(
&mut self.inbound_substream,
Some(InboundSubstreamState::Poisoned),
) {
// inbound idle state
Some(InboundSubstreamState::WaitingInput(mut substream)) => {
match substream.poll_next_unpin(cx) {
Poll::Ready(Some(Ok(message))) => {
self.last_io_activity = Instant::now();
self.inbound_substream =
Some(InboundSubstreamState::WaitingInput(substream));
return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour(message));
}
Poll::Ready(Some(Err(error))) => {
tracing::debug!("Failed to read from inbound stream: {error}");
// Close this side of the stream. If the
// peer is still around, they will re-establish their
// outbound stream i.e. our inbound stream.
self.inbound_substream =
Some(InboundSubstreamState::Closing(substream));
}
// peer closed the stream
Poll::Ready(None) => {
tracing::debug!("Inbound stream closed by remote");
self.inbound_substream =
Some(InboundSubstreamState::Closing(substream));
}
Poll::Pending => {
self.inbound_substream =
Some(InboundSubstreamState::WaitingInput(substream));
break;
}
}
}
Some(InboundSubstreamState::Closing(mut substream)) => {
match Sink::poll_close(Pin::new(&mut substream), cx) {
Poll::Ready(res) => {
if let Err(e) = res {
// Don't close the connection but just drop the inbound substream.
// In case the remote has more to send, they will open up a new
// substream.
tracing::debug!("Inbound substream error while closing: {e}");
}
self.inbound_substream = None;
break;
}
Poll::Pending => {
self.inbound_substream =
Some(InboundSubstreamState::Closing(substream));
break;
}
}
}
None => {
self.inbound_substream = None;
break;
}
Some(InboundSubstreamState::Poisoned) => {
unreachable!("Error occurred during inbound stream processing")
}
}
}

Poll::Pending
}
}
Expand Down

0 comments on commit 22f1e23

Please sign in to comment.