Documentation
¶
Overview ¶
Package batcher groups items added one at a time into batches for a flush function.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrClosed = errors.New("batcher: closed")
ErrClosed is returned by Add after Close.
Functions ¶
This section is empty.
Types ¶
type Batcher ¶
type Batcher[T any] struct { // contains filtered or unexported fields }
Batcher collects items and hands them to flush in batches of at most MaxItems, at the latest MaxWait after the first item of the batch. Each flush runs on its own goroutine, never with the batcher's lock held.
func New ¶
New returns a running batcher that calls flush with each batch. The context passed to flush is canceled when Close is called; Close still flushes the pending items, so flush decides whether to finish the work or fail it.
type Config ¶
type Config struct {
// MaxItems flushes a batch when it holds this many items, and caps the
// size of every batch. 0 flushes on MaxWait only.
MaxItems int
// MaxWait flushes a batch this long after its first item was added. Items
// left over after a batch was taken wait MaxWait again from then.
MaxWait time.Duration
// MaxInFlight caps concurrent flushes. When the cap is reached, Add of an
// item that fills a batch blocks until a flush finishes. 0 means no cap.
MaxInFlight int
}
Config sets when a batch is flushed and how many flushes run at once.