Documentation
¶
Overview ¶
Buffactory is a pre-allocated pool of buffers. Create the BufferFactory struct first. The values in the struct is application dependent, you have to find it ourselve.
bufmaker = &buffactory.BufferFactory{
NumBuffersPerSize: 100,
MinBuffers: 10,
MaxBuffers: 1000,
MinBufferSize: 1024,
MaxBufferSize: 4096,
Reposition: 10 *time.Second,
}
Than init the struct:
err := bufmaker.StartBufferFactory()
if err != nil {
...
}
To request one buffer:
buf := bufmaker.Request(1024)
To return this buffer to the pool:
bufmaker.Return(buf)
Remember to close it in the end:
bufmaker.Close()
Index ¶
Constants ¶
This section is empty.
Variables ¶
var NumSamples int = 1000
Functions ¶
This section is empty.
Types ¶
type BufFactory ¶
type BufferFactory ¶
type BufferFactory struct {
Buffers
//NumBuffersPerSize are the initial number of buffers per size.
NumBuffersPerSize int
// MinBuffers minimal number of buffer per size.
MinBuffers int
// MaxBuffers are the max number of buffer for all sizes.
MaxBuffers int
// MinBufferSize minimal size of a buffer when it is allocated.
MinBufferSize int
// MaxBufferSize max size of a buffer when it is auto allocated.
MaxBufferSize int
// Reposition is the periode of time when the buffer is filled again.
Reposition time.Duration
// contains filtered or unexported fields
}
func (*BufferFactory) RepositionCount ¶
func (bf *BufferFactory) RepositionCount() int
RepositionCount return the number of buffer repositions.
func (*BufferFactory) RequestBuffer ¶
func (bf *BufferFactory) RequestBuffer(size int) *bytes.Buffer
RequestBuffer returns a bytes.Buffer with size size.
func (*BufferFactory) ResetCounters ¶
func (bf *BufferFactory) ResetCounters()
ResetCounters resets the counters.
func (*BufferFactory) ReturnBuffer ¶
func (bf *BufferFactory) ReturnBuffer(buf *bytes.Buffer)
ReturnBuffer is like Return but the argument is bytes.Buffer
func (*BufferFactory) StartBufferFactory ¶
func (bf *BufferFactory) StartBufferFactory() error
StartBufferFactory must be called after the cration of BufferFactory and before the others functions.
type Buffers ¶
type Buffers interface {
// InsertInit inserts a buffer in the pool.
InsertInit(in []byte)
// Return inserts a buffer in the pool.
// If the pool is full drop the buffer, than the
// buffer will be collected by the GC, if there is
// no reference to it.
Return(in []byte)
// Request resturns a buffer with capacity size.
Request(size int) []byte
// NumBuffers resturn the number of buffers in the poll
NumBuffers() int
// Hits returns the number of buffer that was in
// the pool.
Hits() int
// Miss restur the number of requested buffer that
//wasn't in the pool
Miss() int
// Returned returns the number of hits, buffers that
// are put again in the poll and have the same size,
// and miss, buffers that haven't the same size.
Returned() (hit, miss int)
// ResetCounters resets the counters.
ResetCounters()
PrintBuffersStats() string
GetStats() Stats
}
func NewBuffers ¶
NewBuffers return a new struct that holds a collection of buffer classified by size. numbuffers are the initial number of buffers per size. maxbuffers are the max number of buffers for all sizes.