Skip to main content

BatchSubscriber

Trait BatchSubscriber 

Source
pub trait BatchSubscriber: Subscriber {
    type Batch: IntoIterator<Item = <Self as Subscriber>::Message> + Send;

    // Required method
    fn batches(
        &mut self,
        size: NonZeroUsize,
    ) -> impl Stream<Item = Result<Self::Batch, <Self as Subscriber>::Error>> + Send + '_;
}
Expand description

A subscriber that delivers messages in batches.

Every broker offers this: those that batch on the wire (Kafka polls, JetStream pull consumers, Redis XREADGROUP COUNT) translate the batch size into their client’s own parameter, and those whose transport delivers one message at a time assemble batches on the client through the Buffered adapter. The batch a handler sees is exactly the batch the broker delivered: the runtime never splits or merges one.

Required Associated Types§

Source

type Batch: IntoIterator<Item = <Self as Subscriber>::Message> + Send

Container yielded by batches. Implementations choose between Vec, custom iterators, or anything else that yields the underlying Subscriber::Message.

Required Methods§

Source

fn batches( &mut self, size: NonZeroUsize, ) -> impl Stream<Item = Result<Self::Batch, <Self as Subscriber>::Error>> + Send + '_

Returns a stream of batches of at most size messages.

size is the one parameter the framework passes down: it comes from the registration’s batch(n), which every batch handler names. Everything else about how batches are formed - a block timeout, a consumer group, a prefetch window - is the broker’s own business, configured on its subscription source.

A batch must never carry more than size messages; it may carry fewer (that is what a partial batch or a deadline is for). Implementations translate size into whatever their client already speaks (XREADGROUP COUNT, a pull batch size, a poll limit); one with no native batching wraps its subscriber in Buffered, which honours the size on the client.

§Cancel safety

Same guarantees as Subscriber::stream: cancel-safe between polls.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl<Log: LogMode> BatchSubscriber for MemorySubscriber<Log>

Greedy batching: a batch is the first awaited delivery plus everything already buffered, up to the size the registration asked for. Partial batches ship immediately, so no deadline timer is needed - the in-memory transport has nothing to wait for. Each batch is allocated at the requested size up front, so a partial batch holds the room of a full one.

Source§

impl<S: Subscriber> BatchSubscriber for BufferedSubscriber<S>