pool

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package pool provides memory pooling utilities to reduce allocations and improve performance for frequently allocated objects like buffers, readers, writers, and HTTP connections.

Index

Constants

View Source
const (
	SmallBufferSize  = 4 * 1024        // 4KB - headers, small responses
	MediumBufferSize = 64 * 1024       // 64KB - typical responses
	LargeBufferSize  = 1 * 1024 * 1024 // 1MB - large responses
)

Buffer sizes for different use cases.

View Source
const (
	SizeClass512B = 512       // Small headers, short frames
	SizeClass2KB  = 2 * 1024  // Typical header blocks
	SizeClass8KB  = 8 * 1024  // Large header blocks
	SizeClass32KB = 32 * 1024 // Very large responses
)

Size class thresholds for variable-sized allocations. These sizes are optimized for HPACK encoding, frame payloads, and similar hot paths.

Variables

View Source
var (
	ErrPoolClosed    = errors.New("connection pool is closed")
	ErrNoConnection  = errors.New("no available connection")
	ErrConnUnhealthy = errors.New("connection is unhealthy")
)
View Source
var DefaultSizeClassPool = NewSizeClassPool()

Global instance for convenience

Functions

func AppendString

func AppendString(dst []byte, s string) []byte

AppendString appends a string to a byte slice without intermediate allocation.

func BytesToString

func BytesToString(b []byte) string

BytesToString converts a byte slice to a string without allocation. The returned string shares memory with the byte slice. WARNING: Mutating the byte slice after conversion causes undefined behavior.

func BytesToStringCopy

func BytesToStringCopy(b []byte) string

BytesToStringCopy converts a byte slice to a string with allocation. Use this when you need to store the string beyond the lifetime of the byte slice.

func EqualFoldASCII

func EqualFoldASCII(s, t []byte) bool

EqualFold reports whether s and t are equal under Unicode case-folding. This is a fast path for ASCII-only comparisons.

func GetSizeClass

func GetSizeClass(size int) *[]byte

GetSizeClass retrieves a buffer from the default pool.

func GetSizeClassSlice

func GetSizeClassSlice(size int) []byte

GetSizeClassSlice retrieves a buffer slice from the default pool.

func Go

func Go(task func())

Go submits a task to the default pool.

func PutSizeClass

func PutSizeClass(buf *[]byte)

PutSizeClass returns a buffer to the default pool.

func PutSizeClassSlice

func PutSizeClassSlice(buf []byte)

PutSizeClassSlice returns a slice to the default pool.

func Release

func Release()

Release releases the default pool.

func ReleaseRequestArena

func ReleaseRequestArena(a *RequestArena)

ReleaseRequestArena returns a RequestArena to the pool. All slices allocated from the arena are returned to their pools.

func StringToBytes

func StringToBytes(s string) []byte

StringToBytes converts a string to a byte slice without allocation. The returned slice shares memory with the string. WARNING: Mutating the returned slice causes undefined behavior (strings are immutable).

func StringToBytesCopy

func StringToBytesCopy(s string) []byte

StringToBytesCopy converts a string to a byte slice with allocation. Use this when you need to mutate the byte slice.

func Submit

func Submit(task func()) error

Submit submits a task to the default pool.

Types

type BufferPool

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

BufferPool manages byte buffers using sync.Pool for maximum performance. sync.Pool is significantly faster than custom Treiber stack implementations (4.6x single-threaded, 180x parallel according to benchmarks).

func NewBufferPool

func NewBufferPool() *BufferPool

NewBufferPool creates a new buffer pool.

func (*BufferPool) Get

func (p *BufferPool) Get(size int) *[]byte

Get retrieves a buffer of at least the specified size.

func (*BufferPool) Put

func (p *BufferPool) Put(buf *[]byte)

Put returns a buffer to the pool.

func (*BufferPool) ResetStats

func (p *BufferPool) ResetStats()

ResetStats is a no-op (sync.Pool doesn't track stats).

func (*BufferPool) Stats

func (p *BufferPool) Stats() BufferPoolStats

Stats returns pool statistics (placeholder for compatibility).

type BufferPoolStats

type BufferPoolStats struct {
	Gets   uint64
	Puts   uint64
	Allocs uint64
}

BufferPoolStats tracks pool usage (no-op for sync.Pool based impl).

type BytesBufferPool

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

BytesBufferPool manages sync.Pool for bytes.Buffer.

func NewBytesBufferPool

func NewBytesBufferPool(initialSize int) *BytesBufferPool

NewBytesBufferPool creates a new bytes.Buffer pool.

func (*BytesBufferPool) Get

func (p *BytesBufferPool) Get() *bytes.Buffer

Get retrieves a buffer from the pool.

func (*BytesBufferPool) Put

func (p *BytesBufferPool) Put(buf *bytes.Buffer)

Put returns a buffer to the pool.

type ConnPool

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

ConnPool manages host-based connection pooling with LIFO strategy. LIFO (Last-In-First-Out) provides better CPU cache locality than FIFO because recently used connections are more likely to have warm caches.

func NewConnPool

func NewConnPool(cfg PoolConfig) *ConnPool

NewConnPool creates a new connection pool with LIFO strategy.

func (*ConnPool) Close

func (p *ConnPool) Close() error

Close closes the pool and all connections.

func (*ConnPool) CloseConn

func (p *ConnPool) CloseConn(mc *ManagedConn)

CloseConn marks a connection as unhealthy and closes it.

func (*ConnPool) GetConn

func (p *ConnPool) GetConn(ctx context.Context, host string) (*ManagedConn, error)

GetConn retrieves or creates a connection for the host. Uses LIFO strategy to return most recently used connections first.

func (*ConnPool) ReturnConn

func (p *ConnPool) ReturnConn(mc *ManagedConn)

ReturnConn returns a connection to the pool using LIFO strategy.

func (*ConnPool) Stats

func (p *ConnPool) Stats() PoolStats

Stats returns current pool statistics.

type DialFunc

type DialFunc func(ctx context.Context, addr string) (net.Conn, error)

DialFunc is the function type for establishing new connections.

type Headers

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

Headers is a slice-based header storage that avoids map allocations. For typical HTTP requests (< 20 headers), slice iteration is faster than map lookup due to better cache locality and no hashing overhead.

func (*Headers) Add

func (h *Headers) Add(key, value string)

Add adds a header value without replacing existing values.

func (*Headers) Clone

func (h *Headers) Clone() *Headers

Clone creates a deep copy of the headers.

func (*Headers) Del

func (h *Headers) Del(key string)

Del removes a header.

func (*Headers) FromMap

func (h *Headers) FromMap(m map[string][]string)

FromMap populates Headers from a standard map.

func (*Headers) Get

func (h *Headers) Get(key string) string

Get returns the first value for a header key.

func (*Headers) GetAll

func (h *Headers) GetAll(key string) []string

GetAll returns all values for a header key.

func (*Headers) Len

func (h *Headers) Len() int

Len returns the number of distinct header keys.

func (*Headers) Range

func (h *Headers) Range(fn func(key string, values []string) bool)

Range iterates over all headers.

func (*Headers) Release

func (h *Headers) Release()

Release returns the Headers to its pool.

func (*Headers) Reset

func (h *Headers) Reset()

Reset clears all headers for reuse.

func (*Headers) Set

func (h *Headers) Set(key, value string)

Set sets a header value, replacing any existing values.

func (*Headers) ToMap

func (h *Headers) ToMap() map[string][]string

ToMap converts Headers to a standard map (allocates). Use only when interfacing with APIs that require map[string][]string.

type HeadersPool

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

HeadersPool manages Headers object reuse.

func NewHeadersPool

func NewHeadersPool() *HeadersPool

NewHeadersPool creates a new headers pool.

func (*HeadersPool) Get

func (p *HeadersPool) Get() *Headers

Get retrieves a Headers object from the pool.

func (*HeadersPool) Put

func (p *HeadersPool) Put(h *Headers)

Put returns a Headers object to the pool.

type ManagedConn

type ManagedConn struct {
	Conn net.Conn
	// contains filtered or unexported fields
}

ManagedConn wraps a connection with metadata.

func (*ManagedConn) Close

func (mc *ManagedConn) Close() error

Close returns the connection to the pool instead of closing it.

func (*ManagedConn) ForceClose

func (mc *ManagedConn) ForceClose() error

ForceClose actually closes the underlying connection.

func (*ManagedConn) LocalAddr

func (mc *ManagedConn) LocalAddr() net.Addr

LocalAddr returns the local address.

func (*ManagedConn) Read

func (mc *ManagedConn) Read(b []byte) (n int, err error)

Read implements io.Reader for ManagedConn.

func (*ManagedConn) RemoteAddr

func (mc *ManagedConn) RemoteAddr() net.Addr

RemoteAddr returns the remote address.

func (*ManagedConn) SetDeadline

func (mc *ManagedConn) SetDeadline(t time.Time) error

SetDeadline sets the connection deadline.

func (*ManagedConn) SetReadDeadline

func (mc *ManagedConn) SetReadDeadline(t time.Time) error

SetReadDeadline sets the read deadline.

func (*ManagedConn) SetWriteDeadline

func (mc *ManagedConn) SetWriteDeadline(t time.Time) error

SetWriteDeadline sets the write deadline.

func (*ManagedConn) Write

func (mc *ManagedConn) Write(b []byte) (n int, err error)

Write implements io.Writer for ManagedConn.

type PoolConfig

type PoolConfig struct {
	MaxConnsPerHost int
	MaxTotalConns   int
	IdleTimeout     time.Duration
	HealthInterval  time.Duration
	DialFunc        DialFunc
}

PoolConfig holds configuration for the connection pool.

func DefaultPoolConfig

func DefaultPoolConfig() PoolConfig

DefaultPoolConfig returns sensible defaults for high-throughput use.

type PoolStats

type PoolStats struct {
	ActiveConns int64
	HostCount   int
}

Stats returns pool statistics.

type ReaderPool

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

ReaderPool manages bufio.Reader recycling.

func NewReaderPool

func NewReaderPool(bufferSize int) *ReaderPool

NewReaderPool creates a new reader pool with the specified buffer size.

func (*ReaderPool) Get

func (p *ReaderPool) Get(r io.Reader) *bufio.Reader

Get retrieves a reader from the pool and resets it with the given reader.

func (*ReaderPool) Put

func (p *ReaderPool) Put(br *bufio.Reader)

Put returns a reader to the pool.

type RequestArena

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

RequestArena provides arena-like allocation semantics for per-request allocations. All allocations from a RequestArena can be freed together when Free() is called.

Note: Go's experimental arena package was removed. This implementation uses size-class pooling to provide similar benefits without true arena semantics. The API is designed to allow easy migration if arena-like functionality returns to Go in the future.

func AcquireRequestArena

func AcquireRequestArena() *RequestArena

AcquireRequestArena gets a RequestArena from the pool.

func NewRequestArena

func NewRequestArena() *RequestArena

NewRequestArena creates a new per-request allocation arena.

func (*RequestArena) Free

func (a *RequestArena) Free()

Free returns all allocated slices to the pool. After calling Free(), the arena should not be used again.

func (*RequestArena) MakeSlice

func (a *RequestArena) MakeSlice(size int) []byte

MakeSlice allocates a byte slice of the given size. The slice will be returned to the pool when Free() is called.

type RingBuffer

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

RingBuffer is a lock-free ring buffer for streaming body data. It provides better cache locality and lower overhead than channel-based readers.

func (*RingBuffer) Cap

func (rb *RingBuffer) Cap() int

Cap returns the buffer capacity.

func (*RingBuffer) Close

func (rb *RingBuffer) Close() error

Close signals that no more data will be written.

func (*RingBuffer) CloseWithError

func (rb *RingBuffer) CloseWithError(err error) error

CloseWithError closes the buffer with a specific error.

func (*RingBuffer) Len

func (rb *RingBuffer) Len() int

Len returns the number of unread bytes.

func (*RingBuffer) Read

func (rb *RingBuffer) Read(p []byte) (int, error)

Read reads data from the ring buffer. Blocks if buffer is empty (unless closed).

func (*RingBuffer) Release

func (rb *RingBuffer) Release()

Release returns the buffer to its pool.

func (*RingBuffer) Reset

func (rb *RingBuffer) Reset()

Reset clears the ring buffer for reuse.

func (*RingBuffer) Write

func (rb *RingBuffer) Write(data []byte) (int, error)

Write writes data to the ring buffer. Blocks if buffer is full.

type RingBufferPool

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

RingBufferPool manages RingBuffer reuse.

func NewRingBufferPool

func NewRingBufferPool(bufferSize int) *RingBufferPool

NewRingBufferPool creates a new ring buffer pool.

func (*RingBufferPool) Get

func (p *RingBufferPool) Get() *RingBuffer

Get retrieves a ring buffer from the pool.

func (*RingBufferPool) Put

func (p *RingBufferPool) Put(rb *RingBuffer)

Put returns a ring buffer to the pool.

type SizeClassPool

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

SizeClassPool provides granular pooling for variable-sized byte slices. Optimized for hot paths like HPACK encoding and frame serialization.

func NewSizeClassPool

func NewSizeClassPool() *SizeClassPool

NewSizeClassPool creates a new size-class pool.

func (*SizeClassPool) Get

func (p *SizeClassPool) Get(size int) *[]byte

Get retrieves a buffer of at least the specified size. Returns a pointer to a slice; caller should use (*buf)[:n] to set desired length.

func (*SizeClassPool) GetSlice

func (p *SizeClassPool) GetSlice(size int) []byte

GetSlice retrieves a buffer and returns the underlying slice with requested length. This is a convenience method that avoids pointer dereferencing at the call site.

func (*SizeClassPool) GetWithLength

func (p *SizeClassPool) GetWithLength(size int) *[]byte

GetWithLength retrieves a buffer and sets its length to the requested size.

func (*SizeClassPool) Put

func (p *SizeClassPool) Put(buf *[]byte)

Put returns a buffer to the appropriate pool based on its capacity.

func (*SizeClassPool) PutSlice

func (p *SizeClassPool) PutSlice(buf []byte)

PutSlice returns a slice to the pool. Note: Only works if the slice was originally obtained from this pool and hasn't been resliced beyond the original capacity.

type WorkerPool

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

WorkerPool wraps ants.Pool for goroutine pooling. This provides a high-performance goroutine pool that:

  • Reuses goroutines to reduce allocation and scheduling overhead
  • Automatically scales based on load
  • Cleans up idle workers to prevent resource waste
  • Provides 2-6x better performance than unlimited goroutines

func GetDefaultPool

func GetDefaultPool() *WorkerPool

GetDefaultPool returns the default global worker pool.

func NewWorkerPool

func NewWorkerPool(cfg WorkerPoolConfig) (*WorkerPool, error)

NewWorkerPool creates a new goroutine pool using ants.

func (*WorkerPool) Capacity

func (p *WorkerPool) Capacity() int

Capacity returns the maximum number of workers.

func (*WorkerPool) Close

func (p *WorkerPool) Close()

Close is an alias for Release for API compatibility.

func (*WorkerPool) Free

func (p *WorkerPool) Free() int

Free returns the number of available workers.

func (*WorkerPool) Go

func (p *WorkerPool) Go(task func())

Go submits a task and panics if submission fails.

func (*WorkerPool) IsClosed

func (p *WorkerPool) IsClosed() bool

IsClosed returns whether the pool is closed.

func (*WorkerPool) Reboot

func (p *WorkerPool) Reboot()

Reboot restarts a released pool.

func (*WorkerPool) Release

func (p *WorkerPool) Release()

Release closes the pool and releases all resources.

func (*WorkerPool) ReleaseTimeout

func (p *WorkerPool) ReleaseTimeout(timeout time.Duration) error

ReleaseTimeout closes the pool with a timeout for pending tasks.

func (*WorkerPool) Running

func (p *WorkerPool) Running() int

Running returns the number of running workers.

func (*WorkerPool) Submit

func (p *WorkerPool) Submit(task func()) error

Submit submits a task to the pool. Returns error if the pool is closed or overloaded.

func (*WorkerPool) Tune

func (p *WorkerPool) Tune(size int)

Tune dynamically adjusts the pool capacity.

func (*WorkerPool) Waiting

func (p *WorkerPool) Waiting() int

Waiting returns the number of tasks waiting in queue.

type WorkerPoolConfig

type WorkerPoolConfig struct {
	// Capacity is the maximum number of workers.
	// Default: 10000
	Capacity int

	// IdleTimeout is how long an idle worker stays alive.
	// Workers idle longer than this are terminated.
	// Default: 10 seconds
	IdleTimeout time.Duration

	// PreAlloc pre-allocates worker memory for better performance
	// in high-capacity scenarios.
	// Default: false
	PreAlloc bool

	// PanicHandler is called when a task panics.
	// Default: nil (panics are logged but don't crash)
	PanicHandler func(interface{})
}

WorkerPoolConfig configures the worker pool.

func DefaultWorkerPoolConfig

func DefaultWorkerPoolConfig() WorkerPoolConfig

DefaultWorkerPoolConfig returns sensible defaults.

type WriterPool

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

WriterPool manages bufio.Writer recycling.

func NewWriterPool

func NewWriterPool(bufferSize int) *WriterPool

NewWriterPool creates a new writer pool with the specified buffer size.

func (*WriterPool) Get

func (p *WriterPool) Get(w io.Writer) *bufio.Writer

Get retrieves a writer from the pool and resets it with the given writer.

func (*WriterPool) Put

func (p *WriterPool) Put(bw *bufio.Writer)

Put returns a writer to the pool after flushing.

Jump to

Keyboard shortcuts

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