README

bpool GoDoc

Package bpool implements leaky pools of byte arrays and Buffers as bounded channels. It is based on the leaky buffer example from the Effective Go documentation: http://golang.org/doc/effective_go.html#leaky_buffer

bpool provides the following pool types:

  • bpool.BufferPool which provides a fixed-size pool of bytes.Buffers.
  • bpool.BytePool which provides a fixed-size pool of []byte slices with a pre-set width (length).
  • bpool.SizedBufferPool, which is an alternative to bpool.BufferPool that pre-sizes the capacity of buffers issued from the pool and discards buffers that have grown too large upon return.

A common use case for this package is to use buffers to execute HTML templates against (via ExecuteTemplate) or encode JSON into (via json.NewEncoder). This allows you to catch any rendering or marshalling errors prior to writing to a http.ResponseWriter, which helps to avoid writing incomplete or malformed data to the response.

Install

go get github.com/oxtoacart/bpool

Documentation

See godoc.org or use godoc github.com/oxtoacart/bpool

Example

Here's a quick example for using bpool.BufferPool. We create a pool of the desired size, call the Get() method to obtain a buffer for use, and call Put(buf) to return the buffer to the pool.


var bufpool *bpool.BufferPool

func main() {

    bufpool = bpool.NewBufferPool(48)

}

func someFunction() error {

     // Get a buffer from the pool
     buf := bufpool.Get()
     ...
     ...
     ...
     // Return the buffer to the pool
     bufpool.Put(buf)

     return nil
}

License

Apache 2.0 Licensed. See the LICENSE file for details.

Expand ▾ Collapse ▴

Documentation

Overview

    Package bpool implements leaky pools of byte arrays and Buffers as bounded channels. It is based on the leaky buffer example from the Effective Go documentation: http://golang.org/doc/effective_go.html#leaky_buffer

    Index

    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
    }

      BufferPool implements a pool of bytes.Buffers in the form of a bounded channel.

      func NewBufferPool

      func NewBufferPool(size int) (bp *BufferPool)

        NewBufferPool creates a new BufferPool bounded to the given size.

        func (*BufferPool) Get

        func (bp *BufferPool) Get() (b *bytes.Buffer)

          Get gets a Buffer from the BufferPool, or creates a new one if none are available in the pool.

          func (*BufferPool) NumPooled

          func (bp *BufferPool) NumPooled() int

            NumPooled returns the number of items currently pooled.

            func (*BufferPool) Put

            func (bp *BufferPool) Put(b *bytes.Buffer)

              Put returns the given Buffer to the BufferPool.

              type BytePool

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

                BytePool implements a leaky pool of []byte in the form of a bounded channel.

                func NewBytePool

                func NewBytePool(maxSize int, width int) (bp *BytePool)

                  NewBytePool creates a new BytePool bounded to the given maxSize, with new byte arrays sized based on width.

                  func (*BytePool) Get

                  func (bp *BytePool) Get() (b []byte)

                    Get gets a []byte from the BytePool, or creates a new one if none are available in the pool.

                    func (*BytePool) GetSlice

                    func (bp *BytePool) GetSlice() ByteSlice

                      GetSlice implements the method from interface ByteSlicePool

                      func (*BytePool) NumPooled

                      func (bp *BytePool) NumPooled() int

                        NumPooled returns the number of items currently pooled.

                        func (*BytePool) Put

                        func (bp *BytePool) Put(b []byte)

                          Put returns the given Buffer to the BytePool.

                          func (*BytePool) PutSlice

                          func (bp *BytePool) PutSlice(b ByteSlice)

                            PutSlice implements the method from interface ByteSlicePool

                            func (*BytePool) Width

                            func (bp *BytePool) Width() (n int)

                              Width returns the width of the byte arrays in this pool.

                              type ByteSlice

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

                                ByteSlice provides a wrapper around []byte with some added convenience

                                func WrapByteSlice

                                func WrapByteSlice(full []byte, headerLength int) ByteSlice

                                  WrapByteSlice wraps a []byte as a ByteSlice

                                  func (ByteSlice) Bytes

                                  func (b ByteSlice) Bytes() []byte

                                    Bytes returns the current slice

                                    func (ByteSlice) BytesWithHeader

                                    func (b ByteSlice) BytesWithHeader() []byte

                                      BytesWithHeader returns the current slice preceded by the header

                                      func (ByteSlice) Full

                                      func (b ByteSlice) Full() []byte

                                        Full returns the full original buffer underlying the ByteSlice

                                        func (ByteSlice) ResliceTo

                                        func (b ByteSlice) ResliceTo(end int) ByteSlice

                                          ResliceTo reslices the end of the current slice.

                                          type ByteSlicePool

                                          type ByteSlicePool interface {
                                          	// Get gets a byte slice from the pool
                                          	GetSlice() ByteSlice
                                          	// Put returns a byte slice to the pool
                                          	PutSlice(ByteSlice)
                                          	// NumPooled returns the number of currently pooled items
                                          	NumPooled() int
                                          }

                                            ByteSlicePool is a bool of byte slices

                                            func NewByteSlicePool

                                            func NewByteSlicePool(maxSize int, width int) ByteSlicePool

                                              NewByteSlicePool creates a new ByteSlicePool bounded to the given maxSize, with new byte arrays sized based on width

                                              func NewHeaderPreservingByteSlicePool

                                              func NewHeaderPreservingByteSlicePool(maxSize int, width int, headerLength int) ByteSlicePool

                                                NewHeaderPreservingByteSlicePool creates a new ByteSlicePool bounded to the given maxSize, with new byte arrays sized based on width and headerLength preserved at the beginning of the slice.

                                                type SizedBufferPool

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

                                                  SizedBufferPool implements a pool of bytes.Buffers in the form of a bounded channel. Buffers are pre-allocated to the requested size.

                                                  func NewSizedBufferPool

                                                  func NewSizedBufferPool(size int, alloc int) (bp *SizedBufferPool)

                                                    SizedBufferPool creates a new BufferPool bounded to the given size. size defines the number of buffers to be retained in the pool and alloc sets the initial capacity of new buffers to minimize calls to make().

                                                    The value of alloc should seek to provide a buffer that is representative of most data written to the the buffer (i.e. 95th percentile) without being overly large (which will increase static memory consumption). You may wish to track the capacity of your last N buffers (i.e. using an []int) prior to returning them to the pool as input into calculating a suitable alloc value.

                                                    func (*SizedBufferPool) Get

                                                    func (bp *SizedBufferPool) Get() (b *bytes.Buffer)

                                                      Get gets a Buffer from the SizedBufferPool, or creates a new one if none are available in the pool. Buffers have a pre-allocated capacity.

                                                      func (*SizedBufferPool) Put

                                                      func (bp *SizedBufferPool) Put(b *bytes.Buffer)

                                                        Put returns the given Buffer to the SizedBufferPool.