-
Is there a convenient way to implement async batching of messages in a channel? I have already found multistream-batch which does the job but in a sync manner. Meaning, awaiting a batch to be ready is blocking. To be more detailed:
|
Beta Was this translation helpful? Give feedback.
Answered by
Darksonn
Mar 7, 2021
Replies: 2 comments 3 replies
-
Just found futures-batch which looks like doing exactly this job. |
Beta Was this translation helpful? Give feedback.
3 replies
-
Another option is to simply do it manually with let mut batch = Vec::new();
let dur = Duration::new(10, 0);
let mut chan_open = true;
while chan_open {
let timeout = tokio::time::sleep(dur);
tokio::pin!(timeout);
while batch.len() < 25 {
tokio::select! {
item = chan.recv() => match item {
Some(item) => batch.push(item),
None => {
// channel is closed
chan_open = false;
break
}
},
_ = &mut timeout => break,
}
}
handle_batch(&mut batch).await;
batch.clear();
} |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
barkanido
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Another option is to simply do it manually with
tokio::select!
.