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

net: add TcpStream::ready and non-blocking ops #3130

Merged
merged 12 commits into from Nov 13, 2020
Merged

Conversation

carllerche
Copy link
Member

@carllerche carllerche commented Nov 11, 2020

Adds function to await for readiness on the TcpStream and non-blocking read/write functions.

async fn TcpStream::ready(Interest) waits for socket readiness satisfying any of the specified interest. There are also two shorthand functions, readable() and writable().

Once the stream is in a ready state, the caller may perform non-blocking operations on it using try_read() and try_write(). These function return WouldBlock if the stream is not, in fact, ready.

The await readiness function are similar to AsyncFd, but do not require a guard. The guard in AsyncFd protect against a potential race between receiving the readiness notification and clearing it. The guard is needed as Tokio does not control the operations. With TcpStream, the try_read() and try_write() function handle clearing stream readiness as needed.

This also exposes Interest and Ready, both defined in Tokio as wrappers for Mio types. These types will also be useful for fixing #3072 .

Other I/O types, such as TcpListener, UdpSocket, Unix* should get similar functions, but this is left for later PRs.

Refs: #2968

Sketch out splitting up awaiting for readiness and performing the
operation.
@Darksonn Darksonn added A-tokio Area: The main tokio crate C-enhancement Category: A PR with an enhancement or bugfix. M-net Module: tokio/net labels Nov 11, 2020
Copy link
Contributor

@Darksonn Darksonn left a comment

Choose a reason for hiding this comment

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

You add an try_read, but not try_write?

Edit: I see you mention try_write.

tokio/src/net/tcp/stream.rs Outdated Show resolved Hide resolved
@carllerche
Copy link
Member Author

@Darksonn I added try_write now.

@carllerche carllerche marked this pull request as ready for review November 12, 2020 06:23
Copy link
Contributor

@Darksonn Darksonn left a comment

Choose a reason for hiding this comment

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

I haven't looked at everything in this review, but here is some.

tokio/src/io/driver/interest.rs Show resolved Hide resolved
tokio/src/io/driver/interest.rs Show resolved Hide resolved
tokio/src/io/driver/ready.rs Show resolved Hide resolved
@carllerche carllerche merged commit 02b1117 into master Nov 13, 2020
@carllerche carllerche deleted the tcp-stream-readiness branch November 13, 2020 04:07
carllerche added a commit that referenced this pull request Nov 13, 2020
Adds `ready()`, `readable()`, and `writable()` async methods for waiting
for socket readiness. Adds `try_send`, `try_send_to`, `try_recv`, and
`try_recv_from` for performing non-blocking operations on the socket.

This is the UDP equivalent of #3130.
carllerche added a commit that referenced this pull request Nov 16, 2020
Adds `ready()`, `readable()`, and `writable()` async methods for waiting
for socket readiness. Adds `try_send`, `try_send_to`, `try_recv`, and
`try_recv_from` for performing non-blocking operations on the socket.

This is the UDP equivalent of #3130.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio Area: The main tokio crate C-enhancement Category: A PR with an enhancement or bugfix. M-net Module: tokio/net
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants