batchwriter

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2025 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrCapacityTooSmall = errors.New("batch capacity must be at least 1")

ErrCapacityTooSmall is returned when batch capacity is less than 1

View Source
var ErrInvalidFlushInterval = errors.New("flush interval must be at least 1ms")

ErrInvalidFlushInterval is returned when flush interval is less than 1ms

View Source
var ErrNilFlushFunction = errors.New("flush function cannot be nil")

ErrNilFlushFunction is returned when flush function is nil

Functions

This section is empty.

Types

type BatchWriter

type BatchWriter struct {
	// contains filtered or unexported fields
}

BatchWriter handles batch writes with controlled flushing behaviors

func NewBatchWriter

func NewBatchWriter(ctx context.Context, capacity int, flushInterval time.Duration, flushFn FlushFn, opts ...BatchWriterOption) (*BatchWriter, error)

NewBatchWriter creates a new BatchWriter

func (*BatchWriter) Add

func (b *BatchWriter) Add(record any)

Add queues a record for batch writing This method blocks if the input channel is full

func (*BatchWriter) AddWithContext

func (b *BatchWriter) AddWithContext(ctx context.Context, record any) error

AddWithContext attempts to add a record, respecting context cancellation Returns error if context is done or nil on success

func (*BatchWriter) FlushNow

func (b *BatchWriter) FlushNow()

FlushNow forces an immediate flush of the current buffer

func (*BatchWriter) GetMetrics

func (b *BatchWriter) GetMetrics() Metrics

GetMetrics returns the current performance metrics

func (*BatchWriter) SetLogger

func (b *BatchWriter) SetLogger(logger *log.Logger)

SetLogger set logger to be used

func (*BatchWriter) Stop

func (b *BatchWriter) Stop()

Stop gracefully stops the BatchWriter and flushes remaining records This method blocks until all pending records are processed

func (*BatchWriter) TryAdd

func (b *BatchWriter) TryAdd(record any) bool

TryAdd attempts to add a record without blocking Returns true if successful, false if the buffer is full

type BatchWriterOption

type BatchWriterOption func(*BatchWriter)

BatchWriterOption is a functional option for configuring BatchWriter

func WithClearBuffers

func WithClearBuffers(clear bool) BatchWriterOption

WithClearBuffers enables clearing buffer slots after flush

func WithLogger

func WithLogger(logger *log.Logger) BatchWriterOption

WithLogger sets a logger for the BatchWriter

func WithQueueCapacity

func WithQueueCapacity(capacity int) BatchWriterOption

WithQueueCapacity sets the capacity of the input channel This controls how many records can be queued before TryAdd starts failing

type FlushFn

type FlushFn func(records ...any)

FlushFn represents a function that processes a batch of records

type Metrics

type Metrics struct {
	RecordsAdded      uint64        // Total number of records added
	RecordsProcessed  uint64        // Total number of records processed by flush
	RecordsDropped    uint64        // Total number of records dropped (TryAdd failures)
	RecordsInBuffer   uint64        // Current number of records in write buffer
	QueueCapacity     int           // Capacity of the input channel
	FlushCount        uint64        // Number of flushes performed
	LastFlushDuration time.Duration // Duration of the last flush operation
	AvgFlushDuration  time.Duration // Average flush duration
	TotalFlushTime    time.Duration // Total time spent in flush operations
}

Metrics contains the performance metrics for the BatchWriter

Jump to

Keyboard shortcuts

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