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

transports/websocket: Handle websocket CLOSE with reason code #2085

Conversation

dvdplm
Copy link
Contributor

@dvdplm dvdplm commented May 28, 2021

Pass websocket CLOSE reason to the app as an Incoming::Closed(soketto::CloseReason).

This PR is a companion to paritytech/soketto#31 and lets applications know what the remote end claimed to be the reason for closing the connection (if any).

IncomingData is now renamed to Incoming and the actual data (text or binary) is moved into its own Data enum, which in turn allows into_bytes() and the AsRef impl to apply more sanely to something that is actually data.

@dvdplm dvdplm requested a review from mxinden May 28, 2021 16:33
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.

Looks good to me overall.

I have yet to test out how this works in application code (read: substrate), hence the draft status.

Let me know how this goes.

Pong(Vec<u8>)
Pong(Vec<u8>),
/// Close reason.
Closed(CloseReason),
Copy link
Member

Choose a reason for hiding this comment

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

To me, it is still a bit confusing why this is treated as an Event and thus returned via Incoming instead of being treated as an error and thus be returned via Error::Closed.

I am not familiar enough with soketto, thus I don't think this should block the effort.

Background for other readers: paritytech/soketto#29 (review).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's not clear cut what the semantics should be. I went for the Error::Closed approach at first too, but @tomaka pointed out that the closing of the connection does carry information (a code in the simple case, but the specs allow for essentially any amount of "extra data").
The use case I have in mind for this is "collaborative backoff" where friendly peers can use the close reason to ask for respite, e.g. substrate telemetry asking nodes to send less data or go away for a while. In that context I think making the close reason an Error might be unfortunate.

Copy link
Member

@tomaka tomaka Oct 19, 2021

Choose a reason for hiding this comment

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

The general consensus is that whenever a socket/connection produces an error, it means that the connection is no longer in a usable state. This isn't the case after receiving a WebSocket Closed frame. Receiving a closed frame is a normal situation, and the connection is still in a normal state and can be used to send more data or a Closed frame in return.

One drawback is: if Closed was instead an error, we would have a compile-time guarantee to not receive any other message after this closing. This guarantee can no longer be enforced by the compiler, but I think that this is fine.
EDIT: actually no, because the stream could continue to produce non-errors after having produced an error. We're not losing any compile-time guarantee.

Binary(Vec<u8>),
/// UTF-8 encoded application data.
Text(Vec<u8>),
pub enum Incoming {
Copy link
Member

Choose a reason for hiding this comment

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

👍

transports/websocket/src/framed.rs Outdated Show resolved Hide resolved
Co-authored-by: Max Inden <mail@max-inden.de>
@mxinden
Copy link
Member

mxinden commented Jul 8, 2021

Friendly ping @dvdplm.

@mxinden mxinden changed the title Alternative to #2051 transports/websocket: Alternative to #2051 Jul 8, 2021
…n-data-and-notdata' of github.com:libp2p/rust-libp2p into dp-close-reason-as-incoming-with-saner-separation-btween-data-and-notdata
@dvdplm dvdplm marked this pull request as ready for review October 19, 2021 08:43
@dvdplm
Copy link
Contributor Author

dvdplm commented Oct 19, 2021

@mxinden Any chance of this getting in for v0.40?

@dvdplm dvdplm requested a review from tomaka October 19, 2021 08:45
@dvdplm dvdplm changed the title transports/websocket: Alternative to #2051 transports/websocket: Handle websocket CLOSE with reason code Oct 19, 2021
Copy link
Member

@tomaka tomaka left a comment

Choose a reason for hiding this comment

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

Adding Closed to the normal Incoming enum is what makes sense to me

@mxinden
Copy link
Member

mxinden commented Oct 19, 2021

@mxinden Any chance of this getting in for v0.40?

Not into libp2p v0.40.0 since I already published a release candidate, which in the ideal case, will just be promoted to a real release, or amended with bug / security fixes and then followed by another release candidate. Including a new feature in a release candidate would technically force us to test all over again.

That said, I am happy to release libp2p-websocket v0.32.0 once v0.31.0-rc.1 is promoted to v0.31.0.


@dvdplm would you mind including a changelog section for v0.32.0 and an entry for this change?

@dvdplm
Copy link
Contributor Author

dvdplm commented Oct 19, 2021

@dvdplm would you mind including a changelog section for v0.32.0 and an entry for this change?

Happy to do that. In this PR or separately?

@mxinden
Copy link
Member

mxinden commented Oct 19, 2021

This PR please.

@mxinden
Copy link
Member

mxinden commented Oct 30, 2021

Merged via #2319.

@mxinden mxinden closed this Oct 30, 2021
@thomaseizinger thomaseizinger deleted the dp-close-reason-as-incoming-with-saner-separation-btween-data-and-notdata branch November 17, 2022 01:18
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

Successfully merging this pull request may close these issues.

None yet

4 participants