Documentation
¶
Index ¶
Constants ¶
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 ¶
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).
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().