synced

package
v0.7.0 Latest Latest
Warning

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

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

Documentation

Index

Constants

View Source
const PreallocationBatchSize = 1000

PreallocationBatchSize is a sensible default for how many objects are preallocated in a single batch. It's used for both initial and on-demand pool growth.

Variables

This section is empty.

Functions

This section is empty.

Types

type BatchPool

type BatchPool[T any] struct {
	// contains filtered or unexported fields
}

BatchPool is a high-throughput generic object pool with batch preallocation.

The main goal is to: - Minimize allocations by reusing objects. - Reduce allocation spikes by preallocating objects in large batches. - Provide simple Get/Put API similar to sync.Pool but with better bulk allocation behavior.

func NewBatchPool

func NewBatchPool[T any](preallocateBatchSize int, allocFunc func() T) *BatchPool[T]

NewBatchPool creates a new BatchPool with an initial preallocation. - preallocateBatchSize: how many objects to add to the pool per allocation batch. - allocFunc: function to construct a new T.

func (*BatchPool[T]) Get

func (bp *BatchPool[T]) Get() T

Get retrieves an object from the pool, allocating if necessary. Never returns nil (unless allocFunc does).

func (*BatchPool[T]) Len added in v0.7.0

func (bp *BatchPool[T]) Len() int

Len returns the number of objects ever preallocated (does not track live/used count).

func (*BatchPool[T]) Put

func (bp *BatchPool[T]) Put(x T)

Put returns an object to the pool for future reuse.

type FreeResourceFunc

type FreeResourceFunc func()

FreeResourceFunc is a function to release/recycle a pooled resource after use.

type PooledReader

type PooledReader interface {
	// Read reads the response body into a pooled buffer, returning:
	// - bode:   byte slice with the entire response body (may alias an internal buffer)
	// - free:   function to call when you are done with bode (to return buffer to pool)
	// - err:    any error from reading
	Read(resp *http.Response) (bode []byte, free FreeResourceFunc, err error)
}

PooledReader is an interface for reading HTTP responses into pooled byte slices (with explicit free).

type PooledResponseReader

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

PooledResponseReader reads HTTP response bodies into pooled buffers for reuse, minimizing allocations under high load. **Not thread-safe for returned body after free!**

func NewPooledResponseReader

func NewPooledResponseReader(preallocatedBuffers int) *PooledResponseReader

NewPooledResponseReader creates a new PooledResponseReader with a given initial buffer pool size.

func (*PooledResponseReader) Read

func (r *PooledResponseReader) Read(resp *http.Response) (bode []byte, free FreeResourceFunc, err error)

Read reads the HTTP response body into a pooled buffer and returns the bytes. IMPORTANT: The returned bode slice aliases a pooled buffer and must not be used after free() is called. Data race or use-after-free is possible if bode is accessed after calling free().

Jump to

Keyboard shortcuts

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