Documentation
¶
Overview ¶
Package bufferpool implements a capacity-limited pool of reusable, equally-sized buffers.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BufferPool ¶
type BufferPool struct {
// contains filtered or unexported fields
}
A BufferPool is a capacity-limited pool of equally sized buffers.
func New ¶
func New(poolSize, bufferSize int) *BufferPool
New returns a newly allocated BufferPool with the given maximum pool size and buffer size.
Example ¶
package main import ( "fmt" "github.com/pushrax/bufferpool" ) func main() { bp := bufferpool.New(10, 255) dogBuffer := bp.Take() dogBuffer.WriteString("Dog!") bp.Give(dogBuffer) catBuffer := bp.Take() // dogBuffer is reused and reset. catBuffer.WriteString("Cat!") fmt.Println(catBuffer) }
Output: Cat!
func (*BufferPool) Give ¶
func (pool *BufferPool) Give(buf *bytes.Buffer) error
Give is used to attempt to return a buffer to the pool. It may not be added to the pool if it was already full.
func (*BufferPool) GiveSlice ¶
func (pool *BufferPool) GiveSlice(slice []byte) error
GiveSlice is used to attempt to return a slice to the pool. It may not be added to the pool if it was already full.
func (*BufferPool) Take ¶
func (pool *BufferPool) Take() *bytes.Buffer
Take is used to obtain a new zeroed buffer. This will allocate a new buffer if the pool was empty.
func (*BufferPool) TakeSlice ¶
func (pool *BufferPool) TakeSlice() (slice []byte)
TakeSlice is used to obtain a new slice. This will allocate a new slice if the pool was empty.