buffer

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrClosed = errors.New("BatchWriter is closed")

ErrClosed is returned when operations are attempted on a closed BatchWriter

Functions

func GetBuffer

func GetBuffer() *bytes.Buffer

GetBuffer retrieves a buffer from the appropriate global pool based on size hint. This is the default function to use when you need a general-purpose buffer.

Returns:

  • *bytes.Buffer: A medium-sized buffer (512 bytes capacity)

func GetLargeBuffer

func GetLargeBuffer() *bytes.Buffer

GetLargeBuffer retrieves a large buffer optimized for complex content. Use this for structured logging with many fields or large messages.

Returns:

  • *bytes.Buffer: A large buffer (2048 bytes capacity)

func GetSmallBuffer

func GetSmallBuffer() *bytes.Buffer

GetSmallBuffer retrieves a small buffer optimized for short content. Use this for formatting timestamps, log levels, and other small strings.

Returns:

  • *bytes.Buffer: A small buffer (128 bytes capacity)

func GetStringBuilder

func GetStringBuilder() *strings.Builder

GetStringBuilder retrieves a string builder from the global pool. Use this for efficient string concatenation operations.

Returns:

  • *strings.Builder: A string builder with 256 bytes initial capacity

func PutBuffer

func PutBuffer(buf *bytes.Buffer)

PutBuffer returns a buffer to the appropriate global pool. The pool is automatically selected based on the buffer's capacity. Always call this when done with a buffer obtained from GetBuffer.

Parameters:

  • buf: The buffer to return (can be nil)

func PutStringBuilder

func PutStringBuilder(sb *strings.Builder)

PutStringBuilder returns a string builder to the global pool. Always call this when done with a builder obtained from GetStringBuilder.

Parameters:

  • sb: The string builder to return (can be nil)

Types

type BatchWriter

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

BatchWriter implements efficient batched writing with configurable flush triggers.

func NewBatchWriter

func NewBatchWriter(writer *bufio.Writer, maxSize, maxCount int, flushInterval time.Duration) *BatchWriter

NewBatchWriter creates a new batch writer with the specified configuration.

func (*BatchWriter) Close

func (bw *BatchWriter) Close() error

Close flushes any remaining data and stops the timer.

func (*BatchWriter) Flush

func (bw *BatchWriter) Flush() error

Flush forces all buffered data to be written.

func (*BatchWriter) SetBatchSize

func (bw *BatchWriter) SetBatchSize(maxSize, maxCount int)

SetBatchSize updates the maximum batch size.

func (*BatchWriter) SetFlushInterval

func (bw *BatchWriter) SetFlushInterval(interval time.Duration)

SetFlushInterval updates the flush interval.

func (*BatchWriter) Stats

func (bw *BatchWriter) Stats() Stats

Stats returns current batch writer statistics.

func (*BatchWriter) Write

func (bw *BatchWriter) Write(data []byte) (int, error)

Write adds data to the batch buffer and flushes if necessary.

func (*BatchWriter) WriteString

func (bw *BatchWriter) WriteString(data string) (int, error)

WriteString is a convenience method for string data.

type BufferPool

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

BufferPool manages a pool of reusable byte buffers to reduce allocations during log message formatting and writing operations. This significantly improves performance by reducing garbage collection pressure.

func NewBufferPool

func NewBufferPool() *BufferPool

NewBufferPool creates a new buffer pool with a default buffer size. The pool automatically grows and shrinks based on usage patterns. The default capacity is 512 bytes, suitable for most log messages.

Returns:

  • *BufferPool: A new buffer pool instance

func NewBufferPoolWithCapacity

func NewBufferPoolWithCapacity(capacity int) *BufferPool

NewBufferPoolWithCapacity creates a buffer pool with a specific initial capacity. Use this when you know the typical size of your log messages.

Parameters:

  • capacity: Initial buffer capacity in bytes

Returns:

  • *BufferPool: A new buffer pool with the specified capacity

func (*BufferPool) Get

func (bp *BufferPool) Get() *bytes.Buffer

Get retrieves a buffer from the pool or creates a new one if the pool is empty. The caller is responsible for returning the buffer to the pool using Put(). The buffer is automatically reset before being returned.

Returns:

  • *bytes.Buffer: A clean buffer ready for use

func (*BufferPool) Put

func (bp *BufferPool) Put(buf *bytes.Buffer)

Put returns a buffer to the pool for reuse. The buffer is reset before being pooled to prevent data leaks. Extremely large buffers (>32KB) are not pooled to prevent memory bloat.

Parameters:

  • buf: The buffer to return to the pool

type Stats

type Stats struct {
	BufferedEntries int           `json:"buffered_entries"`
	BufferedBytes   int           `json:"buffered_bytes"`
	MaxEntries      int           `json:"max_entries"`
	MaxBytes        int           `json:"max_bytes"`
	FlushInterval   time.Duration `json:"flush_interval"`
}

Stats contains statistics about the batch writer.

type StringBuilderPool

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

StringBuilderPool manages a pool of reusable string builders. String builders are more efficient than buffers for string concatenation.

func NewStringBuilderPool

func NewStringBuilderPool() *StringBuilderPool

NewStringBuilderPool creates a new string builder pool. Builders are pre-allocated with 256 bytes capacity.

Returns:

  • *StringBuilderPool: A new string builder pool

func (*StringBuilderPool) Get

func (sbp *StringBuilderPool) Get() *strings.Builder

Get retrieves a string builder from the pool. The builder is automatically reset before being returned.

Returns:

  • *strings.Builder: A clean string builder ready for use

func (*StringBuilderPool) Put

func (sbp *StringBuilderPool) Put(sb *strings.Builder)

Put returns a string builder to the pool. Large builders (>32KB) are not pooled to prevent memory bloat.

Parameters:

  • sb: The string builder to return to the pool

Jump to

Keyboard shortcuts

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