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/handler: replace inject_* methods #3085

Merged
merged 18 commits into from
Nov 17, 2022

Conversation

jxs
Copy link
Member

@jxs jxs commented Nov 4, 2022

Description

Previously, we had one callback for each kind of message that a ConnectionHandler would receive from either its NetworkBehaviour or the connection itself.

With this patch, we combine these functions, resulting in two callbacks:

  • on_behaviour_event
  • on_connection_event

Resolves #3080.

Links to any relevant issues

ConnectionHandler counterpart to #3011

Open Questions

Change checklist

  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works
  • A changelog entry has been made in the appropriate crates

@jxs jxs marked this pull request as draft November 4, 2022 13:57
@jxs jxs force-pushed the connection-handler-inject-on branch 9 times, most recently from d926cd2 to d60c0e7 Compare November 8, 2022 17:21
@jxs jxs force-pushed the connection-handler-inject-on branch from d60c0e7 to cf9768f Compare November 8, 2022 17:37
Copy link
Member

@mxinden mxinden left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool to see this work happening. Let us know once this is ready for a review.

#[deprecated(
since = "0.41.0",
note = "Handle `StreamEvent::FullyNegotiatedInbound` on `ConnectionHandler::on_event` instead.
the default of implemention of this inject_*` method delegates to it."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
the default of implemention of this inject_*` method delegates to it."
The default implementation of this `inject_*` method delegates to it."

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Max! addressed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool to see this work happening. Let us know once this is ready for a review.

Thanks Max, yeah I am waiting for #3011 to be merged cause this PR also touches some of the same files to then rebase against master and mark it as ready.


/// Enumeration with the list of the possible stream events
/// to pass to [`on_event`](ConnectionHandler::on_event).
pub enum StreamEvent<'a, IP: InboundUpgradeSend, OP: OutboundUpgradeSend, IOI, OOI> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative to explore (not saying it is a good idea) would be to take a single trait parameter, namely ConnectionHandler here.

Suggested change
pub enum StreamEvent<'a, IP: InboundUpgradeSend, OP: OutboundUpgradeSend, IOI, OOI> {
pub enum StreamEvent<'a, Handler: ConnectionHandler> {

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for bringing this Max,
yeah it works but we need to relax the sizedness to Handler: ConnectionHandler + ?Sized because on_event's argument event: StreamEvent<Self> which afaik will probably be problematic if we in the future define methods on ConnectionHandler that take self and StreamEvent<Self> cause Self must be Sized and StreamEvent<Self> may be ?Sized. Would you prefer that over the multiple generic parameters?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah it works but we need to relax the sizedness to Handler: ConnectionHandler + ?Sized because on_event's argument event: StreamEvent<Self> which afaik will probably be problematic if we in the future define methods on ConnectionHandler that take self and StreamEvent<Self> cause Self must be Sized and StreamEvent<Self> may be ?Sized.

I am not sure I am following. In which case would ConnectionHandler not be Sized? In which case would this be problematic?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The approach you suggest on #3085 (comment):

Suggested change
pub enum StreamEvent<'a, IP: InboundUpgradeSend, OP: OutboundUpgradeSend, IOI, OOI> {
pub enum StreamEvent<'a, IP: InboundUpgradeSend, OP: OutboundUpgradeSend, IOI, OOI> {

implies relaxing StreamEvent<Handler> to StreamEvent<Handler: ConnectionHandler + ?Sized>.

Copy link
Contributor

@thomaseizinger thomaseizinger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good! Thank you :)

/// informs the handler about an event coming from the outside in the handler.
fn on_behaviour_event(&mut self, _event: Self::InEvent) {}

fn on_event(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be an on_connection_event maybe? It would be nicer to have both on_ handlers use some kind of qualifier.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah makes sense, thanks Thomas updated! :)

Comment on lines 101 to 127
match event {
StreamEvent::FullyNegotiatedInbound(FullyNegotiatedInbound { protocol, info }) =>
{
#[allow(deprecated)]
self.inner.inject_fully_negotiated_inbound(protocol, info)
}
StreamEvent::FullyNegotiatedOutbound(FullyNegotiatedOutbound { protocol, info }) =>
{
#[allow(deprecated)]
self.inner.inject_fully_negotiated_outbound(protocol, info)
}
StreamEvent::AddressChange(AddressChange { new_address }) =>
{
#[allow(deprecated)]
self.inner.inject_address_change(new_address)
}
StreamEvent::DialUpgradeError(DialUpgradeError { info, error }) =>
{
#[allow(deprecated)]
self.inner.inject_dial_upgrade_error(info, error)
}
StreamEvent::ListenUpgradeError(ListenUpgradeError { info, error }) =>
{
#[allow(deprecated)]
self.inner.inject_listen_upgrade_error(info, error)
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this can be replaced with self.inner.on_event once we remove them right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah exactly, also see #3011 (comment)

use std::task::{Context, Poll};
use void::Void;

use super::{FullyNegotiatedInbound, FullyNegotiatedOutbound, StreamEvent};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer to not use super imports.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, makes total sense, specailly cause we are already importing with crate::handler. Thanks Thomas, updated!


/// Enumeration with the list of the possible stream events
/// to pass to [`on_event`](ConnectionHandler::on_event).
pub enum StreamEvent<'a, IP: InboundUpgradeSend, OP: OutboundUpgradeSend, IOI, OOI> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub enum StreamEvent<'a, IP: InboundUpgradeSend, OP: OutboundUpgradeSend, IOI, OOI> {
pub enum ConnectionEvent<'a, IP: InboundUpgradeSend, OP: OutboundUpgradeSend, IOI, OOI> {

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated!

@jxs jxs marked this pull request as ready for review November 14, 2022 14:00
Copy link
Member

@mxinden mxinden left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor comments. Otherwise this looks good to me.

#[deprecated(
since = "0.41.0",
note = "Handle `ConnectionEvent::FullyNegotiatedInbound` on `ConnectionHandler::on_connection_event` instead.
The default of implemention of this `inject_*` method delegates to it."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The default of implemention of this `inject_*` method delegates to it."
The default implementation of this `inject_*` method delegates to it."

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Max 🙇

@@ -224,6 +277,76 @@ pub trait ConnectionHandler: Send + 'static {
{
ConnectionHandlerSelect::new(self, other)
}

/// informs the handler about an event coming from the outside in the handler.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// informs the handler about an event coming from the outside in the handler.
/// Informs the handler about an event from the [`NetworkBehaviour`].

I would find this easier to understand. What do you think?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah I agree, thanks!

pub enum ConnectionEvent<'a, IP: InboundUpgradeSend, OP: OutboundUpgradeSend, IOI, OOI> {
/// Informs the handler about the output of a successful upgrade on a new inbound substream.
FullyNegotiatedInbound(FullyNegotiatedInbound<IP, IOI>),
/// Informs the handler about successful upgrade on a new outbound stream.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Informs the handler about successful upgrade on a new outbound stream.
/// Informs the handler about the output of a successful upgrade on a new outbound stream.

To be consistent with the above.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah makes sense, thanks Max!

Comment on lines +282 to +294
fn on_behaviour_event(&mut self, _event: Self::InEvent) {}

fn on_connection_event(
&mut self,
_event: ConnectionEvent<
Self::InboundProtocol,
Self::OutboundProtocol,
Self::InboundOpenInfo,
Self::OutboundOpenInfo,
>,
) {
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder, once Self::InEvent is called Self::FromBehaviour, should we rename ConnectionEvent to FromConnection?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works for me, though I suggest not doing that within this pull request.

Copy link
Member

@mxinden mxinden left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the follow-ups.

@mxinden
Copy link
Member

mxinden commented Nov 16, 2022

Happy to merge here. Let's give @thomaseizinger antoher chance to review.

Copy link
Contributor

@thomaseizinger thomaseizinger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@mxinden
Copy link
Member

mxinden commented Nov 17, 2022

@jxs can you take a look at the CI failure?

 error: unresolved link to `NetworkBehaviour`
   --> swarm/src/handler.rs:281:55
    |
281 |     /// Informs the handler about an event from the [`NetworkBehaviour`].
    |                                                       ^^^^^^^^^^^^^^^^ no item named `NetworkBehaviour` in scope
    |
    = note: requested on the command line with `-D rustdoc::broken-intra-doc-links`
    = help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`

error: unresolved link to `ConnectionHandler::on_event`
   --> swarm/src/handler.rs:297:29
    |
297 | /// to pass to [`on_event`](ConnectionHandler::on_event).
    |                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `ConnectionHandler` has no associated item named `on_event`

error: could not document `libp2p-swarm`

https://github.com/libp2p/rust-libp2p/actions/runs/3474448909/jobs/5807623337

@mergify
Copy link
Contributor

mergify bot commented Nov 17, 2022

This pull request has merge conflicts. Could you please resolve them @jxs? 🙏

@mxinden
Copy link
Member

mxinden commented Nov 17, 2022

Just noticed, this is missing changelog entries, right? At least for libp2p-swarm we should have an entry describing the deprecation path. I suggest adding changelog entries to this pull request similar to the ones in #3011.

Can you tackle this @jxs?

@jxs
Copy link
Member Author

jxs commented Nov 17, 2022

Just noticed, this is missing changelog entries, right? At least for libp2p-swarm we should have an entry describing the deprecation path. I suggest adding changelog entries to this pull request similar to the ones in #3011.

Can you tackle this @jxs?

thanks for noticing Max! Yeah added the missing CHANGELOG.md files to the affected crates.

Copy link
Member

@mxinden mxinden left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

@mergify mergify bot merged commit 7803524 into libp2p:master Nov 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

swarm/handler: Inject events via single method
3 participants