Documentation
¶
Overview ¶
Package batchgo collects concurrently submitted items and processes them in bounded, in-memory batches.
A Batcher flushes when a batch reaches Config.MaxSize, when Config.MaxWait elapses after the first item, or when Flush or Close requests a barrier.
Example ¶
package main
import (
"context"
"fmt"
"time"
"github.com/codenaline/batchgo"
)
func main() {
handler := func(_ context.Context, items []int) error {
fmt.Println(items)
return nil
}
batcher, err := batchgo.New(batchgo.Config{
MaxSize: 2,
MaxWait: time.Second,
Workers: 1,
QueueSize: 16,
ErrorQueueSize: 4,
}, handler)
if err != nil {
panic(err)
}
if err := batcher.Add(context.Background(), 1); err != nil {
panic(err)
}
if err := batcher.Add(context.Background(), 2); err != nil {
panic(err)
}
if err := batcher.Close(context.Background()); err != nil {
panic(err)
}
}
Output: [1 2]
Index ¶
- Variables
- type Batcher
- func (b *Batcher[T]) Add(ctx context.Context, item T) error
- func (b *Batcher[T]) Close(ctx context.Context) error
- func (b *Batcher[T]) Done() <-chan struct{}
- func (b *Batcher[T]) DroppedErrors() uint64
- func (b *Batcher[T]) Errors() <-chan error
- func (b *Batcher[T]) Flush(ctx context.Context) error
- func (b *Batcher[T]) TryAdd(item T) error
- type Config
- type Handler
- type PanicError
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrClosed indicates that shutdown has begun and no new command is accepted. ErrClosed = errors.New("batchgo: closed") // ErrQueueFull indicates that TryAdd could not enqueue an item immediately. ErrQueueFull = errors.New("batchgo: queue full") )
Functions ¶
This section is empty.
Types ¶
type Batcher ¶
type Batcher[T any] struct { // contains filtered or unexported fields }
Batcher collects values of type T and delivers completed batches to a Handler. Its methods are safe for concurrent use.
func (*Batcher[T]) Add ¶
Add waits until item is accepted, ctx is canceled, or shutdown begins. A nil return means the item will be dispatched exactly once during this batcher lifetime.
func (*Batcher[T]) Close ¶
Close begins graceful shutdown and waits for all accepted items and active handlers. If ctx expires, graceful shutdown continues and Done eventually closes. Close is safe to call concurrently and repeatedly.
func (*Batcher[T]) Done ¶
func (b *Batcher[T]) Done() <-chan struct{}
Done returns a channel closed after workers stop and Errors is closed.
func (*Batcher[T]) DroppedErrors ¶
DroppedErrors returns the number of errors omitted because Errors was full.
func (*Batcher[T]) Errors ¶
Errors returns the best-effort handler error stream. Reporting never blocks workers; errors are dropped when the configured buffer is full.
type Config ¶
type Config struct {
// MaxSize is the number of items that causes an immediate batch flush.
MaxSize int
// MaxWait is the maximum age of a partial batch, measured from its first item.
MaxWait time.Duration
// Workers is the number of handlers that may run concurrently.
Workers int
// QueueSize bounds item and control commands waiting for the coordinator.
QueueSize int
// ErrorQueueSize bounds errors waiting to be read from Batcher.Errors.
ErrorQueueSize int
}
Config controls batching, worker concurrency, and bounded queue capacities. Every field must be greater than zero.
type Handler ¶
Handler processes one batch. Calls may run concurrently when Config.Workers is greater than one. The item slice is valid only for the duration of the call; a handler that retains data must copy it.
type PanicError ¶
type PanicError struct {
// Value is the value recovered from the handler panic.
Value any
// Stack is the stack captured when the panic was recovered.
Stack []byte
}
PanicError describes a panic recovered from a Handler.
func (*PanicError) Error ¶
func (e *PanicError) Error() string
Error returns a concise description of the recovered panic.