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

Question - Accomulate N messages in T duration or timeout. #529

Open
ajosecueto opened this issue Dec 26, 2022 · 1 comment
Open

Question - Accomulate N messages in T duration or timeout. #529

ajosecueto opened this issue Dec 26, 2022 · 1 comment

Comments

@ajosecueto
Copy link

ajosecueto commented Dec 26, 2022

I would like use poll with a specified timeout but i would like to get N number of messages per call. And poll other messages in the next call.

consumer.iter().take(1000) // Acomulate messages 

I would like to acomulate this messages but based in a specified Duration or Timeout.

@ajosecueto ajosecueto changed the title Question - How to poll N number of messages per call. Question - Accomulate N messages in T duration or timeout. Dec 26, 2022
@marccarre
Copy link

In case it could be useful to @ajosecueto or others, I ended up achieving this using tokio::time::timeout, in a way equivalent to the following:

async fn accumulate_msgs<'a>(
    consumer: &'a StreamConsumer,
    n: usize,
    deadline: std::time::Duration,
) -> Vec<BorrowedMessage<'a>> {
    let mut msgs = Vec::with_capacity(n);
    while msgs.len() < n {
        let msg = match tokio::time::timeout(deadline, consumer.recv()).await {
            Err(e) => {
                debug!("No message received within {deadline:?}: {e}");
                break;
            }
            Ok(result) => match result {
                Ok(msg) => msg,
                Err(e) => {
                    error!("Failed to consume message: {e}");
                    continue;
                }
            },
        };
        msgs.push(msg);
    }
    msgs
}

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

No branches or pull requests

2 participants