Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

swarm/src/connection: Prioritize handler over connection #2626

Merged
merged 2 commits into from
May 3, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 23 additions & 23 deletions swarm/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,28 +116,42 @@ where
(self.handler.into_protocols_handler(), self.muxing.close().0)
}

/// Polls the connection for events produced by the associated handler
/// as a result of I/O activity on the substream multiplexer.
/// Polls the handler and the substream, forwarding events from the former to the latter and
/// vice versa.
pub fn poll(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Result<Event<THandler::OutEvent>, ConnectionError<THandler::Error>>> {
loop {
let mut io_pending = false;
// Poll the handler for new events.
match self.handler.poll(cx) {
Poll::Pending => {}
Poll::Ready(Ok(handler_wrapper::Event::OutboundSubstreamRequest(user_data))) => {
self.muxing.open_substream(user_data);
continue;
}
Poll::Ready(Ok(handler_wrapper::Event::Custom(event))) => {
return Poll::Ready(Ok(Event::Handler(event)));
}
Poll::Ready(Err(err)) => return Poll::Ready(Err(err.into())),
}

// Perform I/O on the connection through the muxer, informing the handler
// of new substreams.
match self.muxing.poll(cx) {
Poll::Pending => io_pending = true,
Poll::Ready(Ok(SubstreamEvent::InboundSubstream { substream })) => self
.handler
.inject_substream(substream, SubstreamEndpoint::Listener),
Poll::Pending => {}
Poll::Ready(Ok(SubstreamEvent::InboundSubstream { substream })) => {
self.handler
.inject_substream(substream, SubstreamEndpoint::Listener);
continue;
}
Poll::Ready(Ok(SubstreamEvent::OutboundSubstream {
user_data,
substream,
})) => {
let endpoint = SubstreamEndpoint::Dialer(user_data);
self.handler.inject_substream(substream, endpoint)
self.handler.inject_substream(substream, endpoint);
continue;
}
Poll::Ready(Ok(SubstreamEvent::AddressChange(address))) => {
self.handler.inject_address_change(&address);
Expand All @@ -146,21 +160,7 @@ where
Poll::Ready(Err(err)) => return Poll::Ready(Err(ConnectionError::IO(err))),
}

// Poll the handler for new events.
match self.handler.poll(cx) {
Poll::Pending => {
if io_pending {
return Poll::Pending; // Nothing to do
}
}
Poll::Ready(Ok(handler_wrapper::Event::OutboundSubstreamRequest(user_data))) => {
self.muxing.open_substream(user_data);
}
Poll::Ready(Ok(handler_wrapper::Event::Custom(event))) => {
return Poll::Ready(Ok(Event::Handler(event)));
}
Poll::Ready(Err(err)) => return Poll::Ready(Err(err.into())),
}
return Poll::Pending;
}
}
}
Expand Down