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: Remove {In,Out}boundUpgrade #4790

Open
Tracked by #2863
thomaseizinger opened this issue Nov 2, 2023 · 0 comments
Open
Tracked by #2863

swarm: Remove {In,Out}boundUpgrade #4790

thomaseizinger opened this issue Nov 2, 2023 · 0 comments

Comments

@thomaseizinger
Copy link
Contributor

thomaseizinger commented Nov 2, 2023

Whilst working on #4789, I had an epiphany. So far, I was of the opinion that we will remove the UpgradeInfo trait together with the {In,Out}boundUpgrade traits.

Doing this always left a somewhat bad taste because the associated types on UpgradeInfo is what allows the entire SelectUpgrade machinery to work:

impl<A, B> UpgradeInfo for SelectUpgrade<A, B>
where
A: UpgradeInfo,
B: UpgradeInfo,
{
type Info = Either<A::Info, B::Info>;
type InfoIter = Chain<
Map<<A::InfoIter as IntoIterator>::IntoIter, fn(A::Info) -> Self::Info>,
Map<<B::InfoIter as IntoIterator>::IntoIter, fn(B::Info) -> Self::Info>,
>;
fn protocol_info(&self) -> Self::InfoIter {
let a = self
.0
.protocol_info()
.into_iter()
.map(Either::Left as fn(A::Info) -> _);
let b = self
.1
.protocol_info()
.into_iter()
.map(Either::Right as fn(B::Info) -> _);
a.chain(b)
}
}

In other words, building a tree of ConnectionHandlers, getting their protocols and dispatching back to the correct handler only works because of the associated type of UpgradeInfo::Info.

To remove some of the complexity of ConnectionHandlers but still retaining this property, we can simply keep the UpgradeInfo trait and just remove the upgrade step. I made a PoC for this in 310311a. It works actually quite well. I've only adjusted the "ping" protocol here:

fn on_connection_event(
&mut self,
event: ConnectionEvent<
<Self::InboundProtocol as UpgradeInfo>::Info,
<Self::OutboundProtocol as UpgradeInfo>::Info,
Self::InboundOpenInfo,
Self::OutboundOpenInfo,
>,
) {
match event {
ConnectionEvent::FullyNegotiatedInbound(FullyNegotiatedInbound {
mut stream,
protocol,
..
}) => {
stream.ignore_for_keep_alive();
self.inbound = Some(protocol::recv_ping(stream).boxed());
}
ConnectionEvent::FullyNegotiatedOutbound(FullyNegotiatedOutbound {
stream: mut stream,
..
}) => {
stream.ignore_for_keep_alive();
self.outbound = Some(OutboundState::Ping(
send_ping(stream, self.config.timeout).boxed(),
));
}
ConnectionEvent::DialUpgradeError(dial_upgrade_error) => {
self.on_dial_upgrade_error(dial_upgrade_error)
}
ConnectionEvent::AddressChange(_)
| ConnectionEvent::ListenUpgradeError(_)
| ConnectionEvent::LocalProtocolsChange(_)
| ConnectionEvent::RemoteProtocolsChange(_) => {}
}
}
}

In essence, this now always received a Stream and the protocol is the one that was negotiated. If a user uses ReadyUpgrade, that will always be an instance of StreamProtocol.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant