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

sync: improve the docs for recv_many #7201

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

SwishSwushPow
Copy link

Trying to explain a bit better when recv_many will return and why in some cases the returned number of received messages is below the input parameter limit.

Motivation

I stumbled upon recv_many for the first time ever in a bit of code while reviewing a PR. I took some issue with how the function was called (inside a while loop) and had a look at the docs to understand why that would be necessary. When reading through the docs it seemed to me as if there was seemingly no need for a loop because recv_many would apparently wait for new messages if none were available, only returning when the limit would be reached or the channel would be closed. I gave this a try in a smaller code example and had to realize that this was not the case. Looking at the source I found that recv_many would only wait for the first message, but would return early if the channel queue had no messages available after that, even when not being closed. At this point I felt that the docs were a bit unclear in that regard.

Solution

By describing the behavior a bit more explicitly I am hoping to improve the docs and avoid misunderstandings like mine in the future.

@github-actions github-actions bot added the R-loom-sync Run loom sync tests on this PR label Mar 7, 2025
@Darksonn Darksonn added T-docs Topic: documentation A-tokio Area: The main tokio crate M-sync Module: tokio/sync labels Mar 7, 2025
Comment on lines +254 to +258
/// * Return when the number of received messages reaches `limit`.
/// * Return earlier when the channel is closed or no further messages area
/// available in the channel at the time. In these cases,
/// the number of received messages can be lower than `limit`.
///
Copy link
Contributor

Choose a reason for hiding this comment

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

If I'm skimming this, I'm going to see "return when the number of received messages reaches limit" and I will get the wrong impression. I agree the current docs are bad, but I don't think this wording is good either.

Copy link
Author

Choose a reason for hiding this comment

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

Could you please give me a hint in which way the current wording isn't up to par? Otherwise I'm not sure in which direction to go with this. 😅

Copy link
Contributor

Choose a reason for hiding this comment

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

How about something like this?

Receives between 1 and `limit` messages into `buffer`.

This method is intended as a performance optimization for cases where
you wish to process messages in batches. The method may return less than
`limit` messages, so `recv_many` can *not* be used to receive a fixed
number of messages in one call.

This method waits until at least one message is available, and then
receives as many messages as it can into `buffer` efficiently. The
number of messages added to `buffer` is returned, and this number will
often be less than `limit` even if the channel is still open.

For non-zero values of `limit`, this method will never return `0` unless
the channel has been closed and there are no remaining messages in the
channel's queue. This indicates that no further values can ever be
received from this `Receiver`. The channel is closed when all senders
have been dropped, or when [`close`] is called.

Note that if [`close`] is called, but there are still outstanding
[`Permits`] from before it was closed, the channel is not considered
closed by `recv_many` until the permits are used or dropped.

The capacity of `buffer` is increased as needed.

Trying to explain a bit better when `recv_many` will return and why in some cases the returned number of received messages is below the input parameter `limit`.

Additionally the docs for the bounded and unbounded `recv_many` are adjusted to match.
@SwishSwushPow SwishSwushPow force-pushed the improve-docs-for-recv-many branch from 5674da6 to 4d665e6 Compare March 7, 2025 10:39
@SwishSwushPow
Copy link
Author

Little bump here. Anything I can do to improve the state of this PR? :)

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 M-sync Module: tokio/sync R-loom-sync Run loom sync tests on this PR T-docs Topic: documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants