Documentation
¶
Index ¶
- Variables
- type BatchWriter
- func (b *BatchWriter) Add(record any)
- func (b *BatchWriter) AddWithContext(ctx context.Context, record any) error
- func (b *BatchWriter) FlushNow()
- func (b *BatchWriter) GetMetrics() Metrics
- func (b *BatchWriter) SetLogger(logger *log.Logger)
- func (b *BatchWriter) Stop()
- func (b *BatchWriter) TryAdd(record any) bool
- type BatchWriterOption
- type FlushFn
- type Metrics
Constants ¶
This section is empty.
Variables ¶
var ErrCapacityTooSmall = errors.New("batch capacity must be at least 1")
ErrCapacityTooSmall is returned when batch capacity is less than 1
var ErrInvalidFlushInterval = errors.New("flush interval must be at least 1ms")
ErrInvalidFlushInterval is returned when flush interval is less than 1ms
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