Documentation
¶
Overview ¶
Package batchman provides an in-memory batching mechanism for items of a given type.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidMaxSize is returned when the max size is invalid. ErrInvalidMaxSize = errors.New("max size must be greater than 0") // ErrInvalidBufferSize is returned when the buffer size is invalid. ErrInvalidBufferSize = errors.New("buffer size must be greater than 0") // ErrInvalidMaxDelay is returned when the max delay is invalid. ErrInvalidMaxDelay = errors.New("max delay must be greater than 0") // ErrBufferFull is returned when the buffer is full. This means the item was not added. ErrBufferFull = errors.New("buffer is full") // ErrBatcherStopped is returned when the batcher has been stopped (through a context cancellation). // No more items can be added. ErrBatcherStopped = errors.New("batcher has been stopped") )
Functions ¶
This section is empty.
Types ¶
type Batcher ¶
type Batcher[T any] struct { // contains filtered or unexported fields }
Batcher is a controller that batches items of a given type into batches with a maximum size or after a maximum delay.
func (*Batcher[T]) CurrentBufferSize ¶
CurrentBufferSize returns the current amount of items in the buffer.
Note that the buffer is not the amount of items pending to be flushed, it doesn't include items currently being flushed or being grouped into the next batch.
You can use this to monitor the buffer size, when the buffer fills up you won't be able to push additional items.
func (*Batcher[T]) Done ¶
func (b *Batcher[T]) Done() <-chan struct{}
Done returns a channel that is closed when the batcher has stopped completely. Once a batcher has stopped, no more items can be pushed, and it can not be started again.
The batcher stops when the parent context is cancelled, but it will flush the remaining items in the buffer. This means that the stopped channel is closed after the last item has been flushed. Depending on the implementation of the flush function, this might take some time.
type Builder ¶
type Builder[T any] struct { // contains filtered or unexported fields }
Builder is a builder for creating a new Batcher.
func New ¶
New creates a new Builder with default values. The default values are a batch size of 1,000, a maximum delay of 10 seconds, and a buffer size of 10,000 items.
func (*Builder[T]) BufferSize ¶
BufferSize sets the buffer size for the batcher.
func (*Builder[T]) Start ¶
func (b *Builder[T]) Start(ctx context.Context, flush func(ctx context.Context, values []T)) (*Batcher[T], error)
Start a new Batcher with the configured values. This returns an error immediately if the configuration is invalid. If the context is cancelled, the Batcher will stop and flush any remaining items.
Directories
¶
| Path | Synopsis |
|---|---|
|
example
|
|
|
graceful_shutdown
command
Package main provides example binaries that demonstrates how the batcher behaves.
|
Package main provides example binaries that demonstrates how the batcher behaves. |
|
readme
command
Package readme contains the code snippet from the README.md file.
|
Package readme contains the code snippet from the README.md file. |
|
stress
command
Package main provides a stress test for the batcher package.
|
Package main provides a stress test for the batcher package. |
|
Package mock provides a mock implementation of a batch handler.
|
Package mock provides a mock implementation of a batch handler. |