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

Redesign upgrade::{write_one, write_with_len_prefix, read_one} #2111

Merged
merged 15 commits into from
Jul 3, 2021

Conversation

thomaseizinger
Copy link
Contributor

@thomaseizinger thomaseizinger commented Jun 25, 2021

This PR re-designs the above three functions to:

  • allow for better composition
  • remove footguns
  • make them easier to use

We achieve this by:

  1. Deprecating the write_one function

Semantically, this function is a composition of write_with_len_prefix and io.close(). This represents a footgun because the close functionality is not obvious and only mentioned in the docs. Using this function multiple times on a single substream will produces hard to debug behaviour.

  1. Deprecating read_one and write_with_len_prefix functions

  2. Introducing write_length_prefixed and read_length_prefixed

  • These functions are symmetric and do exactly what you would expect, just writing to the socket without closing
  • They also have a symmetric interface (no more custom errors, just io::Error)

Even though the docs state it, I've ran into a bug multiple times
where I used `write_one` several times on the same socket, which
obviously doesn't work because it is closed after the first one.
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.

Would you mind including an entry in core/CHANGELOG.md as well?

@thomaseizinger
Copy link
Contributor Author

While we are at it with breaking the interface, I've always found it a bit annoying that read_length_prefixed returns a dedicated error and not just an io::Error. Do you think we could represent the TooLarge variant as one of the representations of io::Error?

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.

While we are at it with breaking the interface, I've always found it a bit annoying that read_length_prefixed returns a dedicated error and not just an io::Error. Do you think we could represent the TooLarge variant as one of the representations of io::Error?

How about ErrorKind::InvalidData? If you decide to apply this change, can you document the ErrorKind::InvalidData return value?

core/CHANGELOG.md Outdated Show resolved Hide resolved
core/src/upgrade/transfer.rs Outdated Show resolved Hide resolved
@thomaseizinger thomaseizinger changed the title Rename write_one to write_one_and_close Redesign upgrade::{write_one, write_with_len_prefix, read_one} Jul 1, 2021
@thomaseizinger
Copy link
Contributor Author

thomaseizinger commented Jul 2, 2021

@mxinden While writing migration docs, I thought it would actually be much better to just not break the API right away and instead deprecate the old functions. So I did just that! The deprecation note should hopefully guide people towards the new functions.

I removed ReadOneError from the new read_length_prefixed function and returned a custom io::Error with a dedicated error message. Do you still want me to document that as part of the functions rustdoc? I think the error message is good enough that it should lead people towards the problem if they ever hit it.

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. Thanks for thorough patch.

I don't quite understand the benefit of deprecating the 3 methods, given that users will have to update at some point anyways and updating should be very low friction. That said, I am fine either way.

Copy link
Contributor Author

@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.

@mxinden Noticed another thing, please hold on with merging.

protocols/floodsub/src/protocol.rs Show resolved Hide resolved
@mxinden mxinden merged commit c1ef4bf into libp2p:master Jul 3, 2021
CHr15F0x added a commit to CHr15F0x/rust-ipfs that referenced this pull request Aug 18, 2021
Includes replacing `upgrade::{write_one, read_one}` with `upgrade::{write_length_prefixed, read_length_prefixed}` respectively, which is a direct consequence of [PR2110](libp2p/rust-libp2p#2111).
CHr15F0x added a commit to CHr15F0x/rust-ipfs that referenced this pull request Aug 18, 2021
Includes replacing `upgrade::{write_one, read_one}` with `upgrade::{write_length_prefixed, read_length_prefixed}` respectively, which is a direct consequence of [PR2111](libp2p/rust-libp2p#2111).
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

2 participants