Skip to content

Conversation

@ADD-SP
Copy link
Member

@ADD-SP ADD-SP commented Aug 17, 2025

Closes #7540

@ADD-SP ADD-SP added T-docs Topic: documentation A-tokio Area: The main tokio crate M-sync Module: tokio/sync labels Aug 17, 2025
@github-actions github-actions bot added the R-loom-sync Run loom sync tests on this PR label Aug 17, 2025
@Darksonn
Copy link
Contributor

I'm not sure this table is the right way to explain this.

Here's the way I think of the method:

  • It returns messages in exactly the same scenarios as recv.
  • But when it returns one message, it will check if more messages are available in that instant, and return those too.

Perhaps we could say that recv_many is a more efficient version of this:

async fn recv_many(&mut self, buffer: &mut Vec<T>, limit: usize) -> usize {
    if limit == 0 {
        return 0;
    }

    // Wait for at least one message (or channel close).
    let Some(first_message) = self.recv().await else {
        return 0;
    };

    buffer.push(first_message);
    let mut num_pushed = 1;

    // Try to get more messages, but don't sleep.
    while num_pushed < limit {
        if let Some(msg) = self.try_recv() {
            buffer.push(msg);
            num_pushed += 1;
        } else {
            break;
        }
    }

    num_pushed
}

@ADD-SP
Copy link
Member Author

ADD-SP commented Aug 18, 2025

I'm not sure this table is the right way to explain this.

Here's the way I think of the method:

* It returns messages in _exactly_ the same scenarios as `recv`.

* But when it returns one message, it will check if more messages are available _in that instant_, and return those too.

Perhaps we could say that recv_many is a more efficient version of this:

async fn recv_many(&mut self, buffer: &mut Vec<T>, limit: usize) -> usize {
    if limit == 0 {
        return 0;
    }

    // Wait for at least one message (or channel close).
    let Some(first_message) = self.recv().await else {
        return 0;
    };

    buffer.push(first_message);
    let mut num_pushed = 1;

    // Try to get more messages, but don't sleep.
    while num_pushed < limit {
        if let Some(msg) = self.try_recv() {
            buffer.push(msg);
            num_pushed += 1;
        } else {
            break;
        }
    }

    num_pushed
}

@Darksonn I believe this is a better way, let me try to shorten this code snippet, and then update the PR.

@ADD-SP ADD-SP force-pushed the add_sp/sync-improve-mpsc-recv-many-docs branch from 071e084 to b30f2a6 Compare August 19, 2025 12:43
@ADD-SP
Copy link
Member Author

ADD-SP commented Aug 19, 2025

I'm not sure this table is the right way to explain this.
Here's the way I think of the method:

* It returns messages in _exactly_ the same scenarios as `recv`.

* But when it returns one message, it will check if more messages are available _in that instant_, and return those too.

Perhaps we could say that recv_many is a more efficient version of this:

async fn recv_many(&mut self, buffer: &mut Vec<T>, limit: usize) -> usize {
    if limit == 0 {
        return 0;
    }

    // Wait for at least one message (or channel close).
    let Some(first_message) = self.recv().await else {
        return 0;
    };

    buffer.push(first_message);
    let mut num_pushed = 1;

    // Try to get more messages, but don't sleep.
    while num_pushed < limit {
        if let Some(msg) = self.try_recv() {
            buffer.push(msg);
            num_pushed += 1;
        } else {
            break;
        }
    }

    num_pushed
}

@Darksonn I believe this is a better way, let me try to shorten this code snippet, and then update the PR.

I think this is already the shortest version, so I just simple copy the above code.

@Darksonn
Copy link
Contributor

I think we should still have some text saying that receives "at least one message".

ADD-SP added 3 commits August 19, 2025 22:05
Signed-off-by: ADD-SP <[email protected]>
Signed-off-by: ADD-SP <[email protected]>
@ADD-SP
Copy link
Member Author

ADD-SP commented Aug 20, 2025

This PR duplicates #7201, I've asked the author to see if they are still going to work on it.

@ADD-SP
Copy link
Member Author

ADD-SP commented Aug 20, 2025

Closed this PR, see #7201 (comment).

@ADD-SP ADD-SP closed this Aug 20, 2025
@ADD-SP ADD-SP deleted the add_sp/sync-improve-mpsc-recv-many-docs branch August 20, 2025 13:41
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.

Documenting behavior of tokio::sync::mpsc::Receiver::recv_many for all cases

2 participants