Crate batch_channel

Source
Expand description

async MPMC channel that reduces overhead by reading and writing many values at once.

Sometimes large volumes of small values are farmed out to workers through a channel. Consider directory traversal: each readdir() call produces a batch of directory entries. Batching can help the consumption side too. Consider querying SQLite with received values – larger batches reduce the amortized cost of acquiring SQLite’s lock and bulk queries can be issued.

One can imagine natural, currently unimplemented, extensions to this crate:

  • Channels with priority
  • impls for futures::sink::Sink and futures::stream::Stream

Ask if they would be helpful.

let (tx, rx) = batch_channel::unbounded();
let tx = tx.into_sync();

tx.send(value).unwrap();
tx.send_iter([v1, v2, v3]).unwrap();

match rx.recv().await {
  Some(value) => println!("single {value}"),
  None => return "sender closed",
}

let batch = rx.recv_batch(100).await;
// batch.is_empty() means sender closed

Structs§

BatchSender
The internal send handle used by Sender::autobatch. Builds a buffer of size batch_limit and flushes when it’s full.
Receiver
The receiving half of a channel. Reads are asynchronous.
SendError
An error returned from Sender::send when all Receivers are dropped.
Sender
The asynchronous sending half of a channel.
SyncBatchSender
Automatically batches up values and sends them when a batch is full.
SyncReceiver
The synchronous receiving half of a channel.
SyncSender
The sending half of a channel.

Functions§

bounded
Allocates a bounded channel and returns the sender, receiver pair.
bounded_sync
Allocates a bounded channel and returns the synchronous handles as a sender, receiver pair.
unbounded
Allocates an unbounded channel and returns the sender, receiver pair.
unbounded_sync
Allocates an unbounded channel and returns the synchronous handles as a sender, receiver pair.