batcher

package
v0.11.16 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2026 License: MIT Imports: 4 Imported by: 3 modules, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

batcher is a package that provides a simple batching mechanism the buffer can be configured with a max capacity and a flush interval the buffer will invoke a callback function when the buffer is full or the flush interval is reached

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Batcher

type Batcher[T any] struct {
	// contains filtered or unexported fields
}

Batcher is a batcher for any type of data

func New

func New[T any](opts ...BatcherOption[T]) *Batcher[T]

New creates a new batcher

func (*Batcher[T]) Append

func (b *Batcher[T]) Append(d ...T)

Append appends data to the batcher

func (*Batcher[T]) Run

func (b *Batcher[T]) Run()

Run starts the batcher. Only the first call has an effect, and a batcher cannot be restarted once stopped.

func (*Batcher[T]) Stop

func (b *Batcher[T]) Stop()

Stop asks the batcher to flush the remaining items and exit, without waiting for it (use WaitDone). It never blocks and may be called any number of times, from any goroutine, including before Run, in which case the batcher flushes and exits as soon as it runs.

Append must not be called concurrently with or after Stop: once the batcher has exited nothing drains it, so Append may block forever (the buffer is full or the size limit is reached) or leave its items unflushed.

func (*Batcher[T]) WaitDone

func (b *Batcher[T]) WaitDone()

WaitDone waits until the batcher is done. It blocks until the batcher has been run and stopped, so it never returns for a batcher that was not run.

type BatcherOption added in v0.0.39

type BatcherOption[T any] func(*Batcher[T])

BatcherOption is the option for the batcher

func WithFlushCallback added in v0.0.39

func WithFlushCallback[T any](fn FlushCallback[T]) BatcherOption[T]

WithFlushCallback sets the flush callback of the batcher

func WithFlushInterval added in v0.0.39

func WithFlushInterval[T any](flushInterval time.Duration) BatcherOption[T]

WithFlushInterval sets the optional flush interval of the batcher

func WithMaxCapacity added in v0.0.39

func WithMaxCapacity[T any](maxCapacity int) BatcherOption[T]

WithMaxCapacity sets the max capacity of the batcher

func WithMaxSize added in v0.2.0

func WithMaxSize[T any](maxSize int32) BatcherOption[T]

WithMaxSize sets the max size of the batcher

type FlushCallback

type FlushCallback[T any] func([]T)

FlushCallback is the callback function that will be called when the batcher is full or the flush interval is reached

Jump to

Keyboard shortcuts

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