bufpool

package
v1.106.0 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2026 License: BSD-3-Clause Imports: 10 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package bufpool provides a pool of variable length buffers.

Buffers retrieved from bufpool can either be used directly with an API similar to bytes.Buffer, or can be used as a raw []byte, by calling the Buffer.Bytes method. Buffers retrieved from the pool must be eventually returned back to the pool. Otherwise, metrics tracking the total number of actively used buffers will be incorrect, and any limits on the maximum number of active buffers will operate incorrectly.

A limit on the number of active buffers may be specified to refuse future attempts to acquire a buffer while running in a high load state. This is useful in server applications as a load shedding mechanism where it is better to fail certain requests instead of crashing.

Example usage:

b, err := Get(n) // n is minimum capacity
if err != nil {
	... // handle err
}
defer Put(b)
... // use b as if it were a bytes.Buffer

Index

Constants

This section is empty.

Variables

View Source
var ErrInsufficientCapacity = errors.New("insufficient capacity to allocate buffer")

ErrInsufficientCapacity indicates that Get and Pool.Get could not acquire a buffer because of size limits (see SetMaxSize or Pool.SetMaxSize).

Functions

func ExpVar

func ExpVar() expvar.Var

ExpVar reports metrics about global pool usage. See Pool.ExpVar.

func Put

func Put(b *Buffer)

Put returns the buffer back to the global pool. The buffer (including any []byte obtained through Buffer.Bytes or Buffer.Next) must not be used any more.

func SetMaxSize

func SetMaxSize(n int64)

SetMaxSize specifies the maximum number of buffer bytes for active use.

Since this has global effects, it is recommended that this only be set in the main package and at a level that's a reasonable fraction of system memory.

See Pool.SetMaxSize.

Types

type Buffer

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

Buffer is like bytes.Buffer, but grows using the global buffer pool. The Grow method returns an error if the buffer cannot be grown. A buffer must be obtained via Get or Pool.Get.

func Get

func Get(n int) (*Buffer, error)

Get retrieves a Buffer with sufficient capacity to hold n bytes. If the global pool lacks sufficient capacity, it returns an error.

Every buffer must be returned to back to the pool by calling Put. Leaking a buffer causes metric gauges about actively used buffers to incorrectly increase without ever decreasing. If SetMaxSize is used, then Get may perpetually fail.

func (*Buffer) Available

func (b *Buffer) Available() int

Available report the unused capacity of the buffer.

func (*Buffer) AvailableBuffer

func (b *Buffer) AvailableBuffer() []byte

AvailableBuffer returns an empty buffer with Buffer.Available capacity. This buffer is intended to be appended to and passed to an immediately succeeding Buffer.Write call. The buffer is only valid until the next write operation on b.

func (*Buffer) Bytes

func (b *Buffer) Bytes() []byte

Bytes returns the underlying buffer.

The caller must ensure that the underlying buffer is no longer in use before putting the Buffer pack in the pool.

func (*Buffer) Cap

func (b *Buffer) Cap() int

Cap reports the capacity of the buffer.

func (*Buffer) Grow

func (b *Buffer) Grow(n int) error

Grow expands the capacity of the buffer to accommodate n additional bytes without any further regrowth.

func (*Buffer) Len

func (b *Buffer) Len() int

Len reports the length of the buffer.

func (*Buffer) Next

func (b *Buffer) Next(n int) []byte

Next returns a slice containing the next n bytes from the buffer, advancing the buffer as if the bytes had been returned by Buffer.Read.

The caller must ensure that the underlying buffer is no longer in use before putting the Buffer pack in the pool.

func (*Buffer) Read

func (b *Buffer) Read(p []byte) (int, error)

Read reads unread bytes from b into p. It implements io.Reader.

func (*Buffer) ReadFrom

func (b *Buffer) ReadFrom(r io.Reader) (n int64, err error)

ReadFrom reads content from r into b. It implements io.ReaderFrom.

func (*Buffer) Reset

func (b *Buffer) Reset()

Reset resets the buffer to be empty, but it retains the underlying storage for use by future writes.

func (*Buffer) String

func (b *Buffer) String() string

String returns the contents of the unread portion of the buffer as a string.

func (*Buffer) Write

func (b *Buffer) Write(p []byte) (int, error)

Write writes the provided bytes from p into b. It implements io.Writer.

func (*Buffer) WriteTo

func (b *Buffer) WriteTo(w io.Writer) (n int64, err error)

WriteTo writes content from b into w. It implements io.WriterTo.

type Pool

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

Pool is a pool of Buffers.

func NewPool

func NewPool() *Pool

NewPool constructs a new buffer pool. By default, there is no limit to the maximum bytes in active use. See Pool.SetMaxSize.

func (*Pool) ExpVar

func (p *Pool) ExpVar() expvar.Var

ExpVar reports metrics about pool usage:

  • gauge_used_bytes: buffer bytes in active use
  • gauge_used_buffers: number of buffers in active use
  • counter_total_used_bytes: total buffer bytes ever acquired
  • counter_total_used_buffers: total number of buffers ever acquired
  • counter_total_refused_buffers: total number of buffers refused due to a limit on the total number of active buffer bytes
  • gauge_used_buffers_by_level: a map of the number of buffers in active use keyed by the log2 of the buffer size
  • counter_total_used_buffers_by_level: a map of the total number of buffers ever acquired keyed by the log2 of the buffer size

func (*Pool) Get

func (p *Pool) Get(n int) (*Buffer, error)

Get retrieves a Buffer with sufficient capacity to hold n bytes. If the pool lacks sufficient capacity, it returns an error.

Every buffer must be returned to back to the pool by calling Pool.Put. Leaking a buffer causes metric gauges about actively used buffers to incorrectly increase without ever decreasing. If Pool.SetMaxSize is used, then Pool.Get may perpetually fail.

func (*Pool) Put

func (p *Pool) Put(b *Buffer)

Put returns the buffer back to the pool. The buffer (including any []byte obtained through Buffer.Bytes or Buffer.Next) must not be used any more.

func (*Pool) SetMaxSize

func (p *Pool) SetMaxSize(n int64)

SetMaxSize specifies the maximum number of buffer bytes for active use.

Setting this to a lower limit does not affect active buffers, but prevents Pool.Get from successfully returning until enough buffers have been Pool.Put that reduces the total active buffers back to the limit.

A negative value specifies no limit. By default, there is no limit.

Jump to

Keyboard shortcuts

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