Help us shape the future of Go! Take the 2026 Go Developer Survey

batcher

package
v1.27.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 14, 2026 License: MIT Imports: 2 Imported by: 0

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

func (b *Batcher[T, K]) Add(item T) bool

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.

func (*Batcher[T, K]) Batches

func (b *Batcher[T, K]) Batches() <-chan []T

Batches returns the channel batches are delivered on. It is closed by Close.

func (*Batcher[T, K]) Close

func (b *Batcher[T, K]) Close()

Close emits all pending batches and closes the Batches channel. Items added after Close are dropped. Close is idempotent.

type Option

type Option func(*options)

func WithMaxBatch

func WithMaxBatch(n int) Option

WithMaxBatch sets the maximum number of items buffered per key within a window. Items beyond it are dropped.

func WithMaxWindow

func WithMaxWindow(d time.Duration) Option

WithMaxWindow sets the upper bound for the window growth. It is raised to WithWindow if smaller.

func WithOutputCapacity

func WithOutputCapacity(n int) Option

WithOutputCapacity sets the capacity of the Batches channel. Batches are dropped when it is full.

func WithPassThrough

func WithPassThrough(n int) Option

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

func WithWindow(d time.Duration) Option

WithWindow sets the initial window for a key.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL