buffactory

package module
v0.0.0-...-9bafcc4 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2015 License: Apache-2.0 Imports: 8 Imported by: 1

README

Buffactory

Buffactory is a pre-allocated pool of buffers.

Build Status GoDoc

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

View Source
var NumSamples int = 1000

Functions

This section is empty.

Types

type BufFactory

type BufFactory interface {
	Buffers
	RequestBuffer(size int) (buf *bytes.Buffer)
	ReturnBuffer(buf *bytes.Buffer)
	Close()
}

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) Close

func (bf *BufferFactory) Close()

Close closes the factory

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

func NewBuffers(numbuffers, maxbuffers int) Buffers

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.

type Stats

type Stats []int64

func (Stats) Average

func (s Stats) Average() float64

func (*Stats) InsertData

func (s *Stats) InsertData(val int)

func (Stats) Max

func (s Stats) Max() int64

func (Stats) Min

func (s Stats) Min() int64

func (Stats) StdDev

func (s Stats) StdDev() float64

func (Stats) String

func (s Stats) String() string

Jump to

Keyboard shortcuts

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