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 ¶
- Variables
- func ExpVar() expvar.Var
- func Put(b *Buffer)
- func SetMaxSize(n int64)
- type Buffer
- func (b *Buffer) Available() int
- func (b *Buffer) AvailableBuffer() []byte
- func (b *Buffer) Bytes() []byte
- func (b *Buffer) Cap() int
- func (b *Buffer) Grow(n int) error
- func (b *Buffer) Len() int
- func (b *Buffer) Next(n int) []byte
- func (b *Buffer) Read(p []byte) (int, error)
- func (b *Buffer) ReadFrom(r io.Reader) (n int64, err error)
- func (b *Buffer) Reset()
- func (b *Buffer) String() string
- func (b *Buffer) Write(p []byte) (int, error)
- func (b *Buffer) WriteTo(w io.Writer) (n int64, err error)
- type Pool
Constants ¶
This section is empty.
Variables ¶
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 ¶
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 ¶
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) AvailableBuffer ¶
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 ¶
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) Grow ¶
Grow expands the capacity of the buffer to accommodate n additional bytes without any further regrowth.
func (*Buffer) Next ¶
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) ReadFrom ¶
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 ¶
String returns the contents of the unread portion of the buffer as a string.
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 ¶
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 ¶
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 ¶
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 ¶
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.