Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/server/coalescer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ impl PollCoalescer {
tokio::task::Builder::new()
.name("poll_coalescer")
.spawn(async move {
let mut batch: Vec<PendingPoll> = Vec::with_capacity(cfg.max_batch_size);

loop {
// wait until there are pending polls
if pending.lock().await.is_empty() {
Expand All @@ -72,8 +74,7 @@ impl PollCoalescer {
tokio::time::sleep(Duration::from_millis(cfg.batch_window_ms)).await;

// get a batch of pending polls
// TODO: reuse across loops
let mut batch: Vec<PendingPoll> = Vec::new();
batch.clear();
{
let mut pending = pending.lock().await;
let take_n = pending.len().min(cfg.max_batch_size);
Expand All @@ -97,7 +98,7 @@ impl PollCoalescer {
total_items = total_items.min(cfg.max_batch_items);

if total_items == 0 {
for p in batch {
for p in batch.drain(..) {
let _ = p.tx.send(Ok(None));
}
continue;
Expand All @@ -110,7 +111,7 @@ impl PollCoalescer {
Ok(v) => v,
Err(e) => {
let shared = std::sync::Arc::new(e);
for p in batch {
for p in batch.drain(..) {
let _ = p.tx.send(Err(Arc::clone(&shared)));
}
continue;
Expand Down Expand Up @@ -142,7 +143,7 @@ impl PollCoalescer {
}
}

for (pos, p) in batch.into_iter().enumerate() {
for (pos, p) in batch.drain(..).enumerate() {
let items_for_req = std::mem::take(&mut distributed[pos]);
let _ = if items_for_req.is_empty() {
p.tx.send(Ok(None))
Expand Down