bufferpool

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package bufferpool is a tiny sync.Pool wrapper specialised for []byte scratch buffers. We expose Get/Put rather than a typed Buffer so callers can resize freely (append) and the pool just stores the largest backing array each goroutine has produced.

Optional soft cap on the number of buffers checked out at once: SetMaxLive(N) — when N > 0 and the in-flight count is at the cap, Get returns a fresh []byte that bypasses sync.Pool. This keeps a 10,000-concurrent-connection burst from expanding the sync.Pool's backing slab past ~N × 32 KiB. Non-pooled buffers feed back through Put (we cannot distinguish them), so the counter drifts slightly during bursts but self-corrects — what matters is that sync.Pool never receives a Get call beyond the cap, so the steady-state working set stays bounded.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Get

func Get() *[]byte

Get returns a zero-length slice with at least defaultCap capacity. Under load past the configured live cap, Get returns a fresh buffer that bypasses sync.Pool instead of growing the pool further.

func Live

func Live() int

Live reports the approximate in-flight count (for metrics).

func Put

func Put(bp *[]byte)

Put returns the buffer to the pool. The caller must not retain a reference. Buffers larger than 1 MiB are dropped to avoid pinning memory after a burst of large responses.

func SetMaxLive

func SetMaxLive(n int)

SetMaxLive sets the soft cap on in-flight buffers. 0 disables the cap. When the cap is hit, Get allocates fresh instead of going through sync.Pool; Put is unchanged. This is an operability knob, not a correctness one — callers always receive a usable buffer.

Types

This section is empty.

Jump to

Keyboard shortcuts

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