rust-rdkafka icon indicating copy to clipboard operation
rust-rdkafka copied to clipboard

How to receive message or timeout

Open ravieze opened this issue 1 year ago • 1 comments

Hi,

I have subscribed to a topic. I would like to receive a message within the next 2 sec or else I want to timeout. I am unable to find a suitable method. Any example code will be helpful.

I see streamConsumer.recv() --> but this is blocking a call.

ravieze avatar May 04 '23 12:05 ravieze

Hi, I faced the exact same issue some days ago and I came up with this solution:

let message = match tokio::time::timeout(timeout, consumer.recv()).await {
    Ok(Ok(message)) => Ok(Some(message)),
    Ok(Err(err)) => Err(err.to_string()),
    Err(_) => Ok(None),
}?;

Ofc this needs tokio crate

ianFar96 avatar May 16 '23 19:05 ianFar96