Documentation
¶
Overview ¶
Package batcher batches items by key with a growing window.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Batcher ¶
type Batcher[T any, K comparable] struct { // contains filtered or unexported fields }
Batcher groups items by key. The first passThrough items for a key are emitted immediately, subsequent items are buffered and emitted as a batch when the key's window expires. Every window that emits a batch doubles the next one (up to maxWindow); a window with nothing buffered resets the key. Batches are delivered on Batches.
func New ¶
func New[T any, K comparable]( keyFn func(T) K, opts ...Option, ) *Batcher[T, K]
New creates a Batcher, panics on invalid arguments.
func (*Batcher[T, K]) Add ¶
Add adds an item and never blocks. It returns false if the item was dropped because the key's batch or the output channel was full, or b was closed.
type Option ¶
type Option func(*options)
func WithMaxBatch ¶
WithMaxBatch sets the maximum number of items buffered per key within a window. Items beyond it are dropped.
func WithMaxWindow ¶
WithMaxWindow sets the upper bound for the window growth. It is raised to WithWindow if smaller.
func WithOutputCapacity ¶
WithOutputCapacity sets the capacity of the Batches channel. Batches are dropped when it is full.
func WithPassThrough ¶
WithPassThrough sets the number of items per key that are emitted immediately before batching is applied. The count resets after a window in which nothing was buffered, so keys with at most n items per window are never batched. Must be >= 1.
func WithWindow ¶
WithWindow sets the initial window for a key.