pool

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2021 License: Apache-2.0 Imports: 13 Imported by: 142

Documentation

Overview

Package pool is a generated GoMock package.

Package pool provides implementations for object pooling.

Index

Examples

Constants

View Source
const (

	// DynamicPoolSize is a magic size value that will force use of sync.Pool-backed pool implementation.
	DynamicPoolSize = -1
)

Variables

This section is empty.

Functions

func AppendByte

func AppendByte(bytes []byte, b byte, pool BytesPool) []byte

AppendByte appends a byte to a byte slice getting a new slice from the BytesPool if the slice is at capacity

func AppendByteChecked

func AppendByteChecked(
	bytes checked.Bytes,
	b byte,
	pool CheckedBytesPool,
) (
	result checked.Bytes,
	swapped bool,
)

AppendByteChecked appends a byte to a byte slice getting a new slice from the CheckedBytesPool if the slice is at capacity

Types

type Allocator

type Allocator func() interface{}

Allocator allocates an object for a pool.

type Bucket

type Bucket struct {
	// Capacity is the size of each element in the bucket.
	Capacity int

	// Count is the number of fixed elements in the bucket.
	Count Size

	// Options is an optional override to specify options to use for a bucket,
	// specify nil to use the options specified to the bucketized pool
	// constructor for this bucket.
	Options ObjectPoolOptions
}

Bucket specifies a pool bucket.

type BucketByCapacity

type BucketByCapacity []Bucket

BucketByCapacity is a sortable collection of pool buckets.

func (BucketByCapacity) Len

func (x BucketByCapacity) Len() int

func (BucketByCapacity) Less

func (x BucketByCapacity) Less(i, j int) bool

func (BucketByCapacity) Swap

func (x BucketByCapacity) Swap(i, j int)

type BucketConfiguration

type BucketConfiguration struct {
	// The count of the items in the bucket.
	Count Size `yaml:"count"`

	// The capacity of each item in the bucket.
	Capacity int `yaml:"capacity"`
}

BucketConfiguration contains configuration for a pool bucket.

func (*BucketConfiguration) NewBucket

func (c *BucketConfiguration) NewBucket() Bucket

NewBucket creates a new bucket.

type BucketizedAllocator

type BucketizedAllocator func(capacity int) interface{}

BucketizedAllocator allocates an object for a bucket given its capacity.

type BucketizedObjectPool

type BucketizedObjectPool interface {
	// Init initializes the pool.
	Init(alloc BucketizedAllocator)

	// Get provides an object from the pool.
	Get(capacity int) interface{}

	// Put returns an object to the pool, given the object capacity.
	Put(obj interface{}, capacity int)
}

BucketizedObjectPool is a bucketized pool of objects.

func NewBucketizedObjectPool

func NewBucketizedObjectPool(sizes []Bucket, opts ObjectPoolOptions) BucketizedObjectPool

NewBucketizedObjectPool creates a bucketized object pool

type BucketizedPoolConfiguration

type BucketizedPoolConfiguration struct {
	// The pool bucket configuration.
	Buckets []BucketConfiguration `yaml:"buckets"`

	// The watermark configuration.
	Watermark WatermarkConfiguration `yaml:"watermark"`
}

BucketizedPoolConfiguration contains configuration for bucketized pools.

func (*BucketizedPoolConfiguration) NewBuckets

func (c *BucketizedPoolConfiguration) NewBuckets() []Bucket

NewBuckets create a new list of buckets.

func (*BucketizedPoolConfiguration) NewObjectPoolOptions

func (c *BucketizedPoolConfiguration) NewObjectPoolOptions(
	instrumentOpts instrument.Options,
) ObjectPoolOptions

NewObjectPoolOptions creates a new set of object pool options.

type BytesPool

type BytesPool interface {
	// Init initializes the pool.
	Init()

	// Get provides a buffer from the pool.
	Get(capacity int) []byte

	// Put returns a buffer to the pool.
	Put(buffer []byte)
}

BytesPool provides a pool for variable size buffers.

func NewBytesPool

func NewBytesPool(sizes []Bucket, opts ObjectPoolOptions) BytesPool

NewBytesPool creates a new bytes pool

type CheckedBytesPool

type CheckedBytesPool interface {
	// Init initializes the pool.
	Init()

	// Get provides a buffer from the pool, to return it to the pool simply
	// increment it immediately, continue to increment and decrement through
	// use and when decremented to zero and finalized it will return itself
	// to the pool. The pool uses the finalizer on the checked.Bytes so be sure
	// not to override it.
	Get(capacity int) checked.Bytes

	// BytesPool returns the underlying bytes pool used, useful if required
	// to retrieve when only the checked bytes pool is accessible.
	BytesPool() BytesPool
}

CheckedBytesPool provides a checked pool for variable size buffers.

func NewCheckedBytesPool

func NewCheckedBytesPool(
	sizes []Bucket,
	opts ObjectPoolOptions,
	newBackingBytesPool NewBytesPoolFn,
) CheckedBytesPool

NewCheckedBytesPool creates a new checked bytes pool

type FloatsPool

type FloatsPool interface {
	// Init initializes the pool.
	Init()

	// Get provides an float64 slice from the pool.
	Get(capacity int) []float64

	// Put returns an float64 slice to the pool.
	Put(value []float64)
}

FloatsPool provides a pool for variable-sized float64 slices.

func NewFloatsPool

func NewFloatsPool(sizes []Bucket, opts ObjectPoolOptions) FloatsPool

NewFloatsPool creates a new floats pool

type MockBytesPool

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

MockBytesPool is a mock of BytesPool interface.

func NewMockBytesPool

func NewMockBytesPool(ctrl *gomock.Controller) *MockBytesPool

NewMockBytesPool creates a new mock instance.

func (*MockBytesPool) EXPECT

EXPECT returns an object that allows the caller to indicate expected use.

func (*MockBytesPool) Get

func (m *MockBytesPool) Get(arg0 int) []byte

Get mocks base method.

func (*MockBytesPool) Init

func (m *MockBytesPool) Init()

Init mocks base method.

func (*MockBytesPool) Put

func (m *MockBytesPool) Put(arg0 []byte)

Put mocks base method.

type MockBytesPoolMockRecorder

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

MockBytesPoolMockRecorder is the mock recorder for MockBytesPool.

func (*MockBytesPoolMockRecorder) Get

func (mr *MockBytesPoolMockRecorder) Get(arg0 interface{}) *gomock.Call

Get indicates an expected call of Get.

func (*MockBytesPoolMockRecorder) Init

Init indicates an expected call of Init.

func (*MockBytesPoolMockRecorder) Put

func (mr *MockBytesPoolMockRecorder) Put(arg0 interface{}) *gomock.Call

Put indicates an expected call of Put.

type MockCheckedBytesPool

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

MockCheckedBytesPool is a mock of CheckedBytesPool interface.

func NewMockCheckedBytesPool

func NewMockCheckedBytesPool(ctrl *gomock.Controller) *MockCheckedBytesPool

NewMockCheckedBytesPool creates a new mock instance.

func (*MockCheckedBytesPool) BytesPool

func (m *MockCheckedBytesPool) BytesPool() BytesPool

BytesPool mocks base method.

func (*MockCheckedBytesPool) EXPECT

EXPECT returns an object that allows the caller to indicate expected use.

func (*MockCheckedBytesPool) Get

func (m *MockCheckedBytesPool) Get(arg0 int) checked.Bytes

Get mocks base method.

func (*MockCheckedBytesPool) Init

func (m *MockCheckedBytesPool) Init()

Init mocks base method.

type MockCheckedBytesPoolMockRecorder

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

MockCheckedBytesPoolMockRecorder is the mock recorder for MockCheckedBytesPool.

func (*MockCheckedBytesPoolMockRecorder) BytesPool

func (mr *MockCheckedBytesPoolMockRecorder) BytesPool() *gomock.Call

BytesPool indicates an expected call of BytesPool.

func (*MockCheckedBytesPoolMockRecorder) Get

func (mr *MockCheckedBytesPoolMockRecorder) Get(arg0 interface{}) *gomock.Call

Get indicates an expected call of Get.

func (*MockCheckedBytesPoolMockRecorder) Init

Init indicates an expected call of Init.

type NewBytesPoolFn

type NewBytesPoolFn func(sizes []Bucket) BytesPool

NewBytesPoolFn is a function to construct a new bytes pool

type ObjectPool

type ObjectPool interface {
	// Init initializes the pool.
	Init(alloc Allocator)

	// Get provides an object from the pool.
	Get() interface{}

	// Put returns an object to the pool.
	Put(obj interface{})
}

ObjectPool provides a pool for objects.

Example
package main

import (
	"fmt"

	"github.com/m3db/m3/src/x/pool"
)

type exampleObject struct {
	a, b, c int64
}

func (o *exampleObject) reset() {
	o.a = 0
	o.b = 0
	o.c = 0
}

func main() {
	opts := pool.NewObjectPoolOptions()
	p := pool.NewObjectPool(opts)
	p.Init(func() interface{} {
		// The Pool's Allocator should generally only return pointer
		// types, since a pointer can be put into the return interface
		// value without an allocation.
		return new(exampleObject)
	})

	// Get an exampleObject from the pool.
	o := p.Get().(*exampleObject)

	fmt.Printf("Retrieved struct should have default values: %+v", o)
	
Output:

func NewObjectPool

func NewObjectPool(opts ObjectPoolOptions) ObjectPool

NewObjectPool creates a new pool

type ObjectPoolConfiguration

type ObjectPoolConfiguration struct {
	// The size of the pool.
	Size Size `yaml:"size"`

	// The watermark configuration.
	Watermark WatermarkConfiguration `yaml:"watermark"`
}

ObjectPoolConfiguration contains configuration for object pools.

func (*ObjectPoolConfiguration) NewObjectPoolOptions

func (c *ObjectPoolConfiguration) NewObjectPoolOptions(
	instrumentOpts instrument.Options,
) ObjectPoolOptions

NewObjectPoolOptions creates a new set of object pool options.

type ObjectPoolOptions

type ObjectPoolOptions interface {
	// SetSize sets the size of the object pool.
	SetSize(value int) ObjectPoolOptions

	// Size returns the size of the object pool.
	Size() int

	// SetDynamic creates a dynamically-sized, non-preallocated pool.
	SetDynamic(value bool) ObjectPoolOptions

	// Dynamic returns if the pool is dynamic.
	Dynamic() bool

	// SetRefillLowWatermark sets the refill low watermark value between [0, 1),
	// if zero then no refills occur.
	SetRefillLowWatermark(value float64) ObjectPoolOptions

	// RefillLowWatermark returns the refill low watermark value between [0, 1),
	// if zero then no refills occur.
	RefillLowWatermark() float64

	// SetRefillHighWatermark sets the refill high watermark value between [0, 1),
	// if less or equal to low watermark then no refills occur.
	SetRefillHighWatermark(value float64) ObjectPoolOptions

	// RefillLowWatermark returns the refill low watermark value between [0, 1),
	// if less or equal to low watermark then no refills occur.
	RefillHighWatermark() float64

	// SetInstrumentOptions sets the instrument options.
	SetInstrumentOptions(value instrument.Options) ObjectPoolOptions

	// InstrumentOptions returns the instrument options.
	InstrumentOptions() instrument.Options

	// SetOnPoolAccessErrorFn sets the on pool access error callback, by
	// default this is a panic.
	SetOnPoolAccessErrorFn(value OnPoolAccessErrorFn) ObjectPoolOptions

	// OnPoolAccessErrorFn returns the on pool access error callback, by
	// default this is a panic.
	OnPoolAccessErrorFn() OnPoolAccessErrorFn
}

ObjectPoolOptions provides options for an object pool.

func NewObjectPoolOptions

func NewObjectPoolOptions() ObjectPoolOptions

NewObjectPoolOptions creates a new set of object pool options

type OnPoolAccessErrorFn

type OnPoolAccessErrorFn func(err error)

OnPoolAccessErrorFn is a function to call when a pool access error occurs, such as get or put before the pool is initialized.

type Size added in v1.2.0

type Size int

Size stores pool capacity for pools that can be either dynamic or pre-allocated

func (Size) IsDynamic added in v1.2.0

func (s Size) IsDynamic() bool

IsDynamic returns whether the pool should be fixed size or not.

func (*Size) UnmarshalText added in v1.2.0

func (s *Size) UnmarshalText(b []byte) error

UnmarshalText unmarshals Size.

type WatermarkConfiguration

type WatermarkConfiguration struct {
	// The low watermark to start refilling the pool, if zero none.
	RefillLowWatermark float64 `yaml:"low" validate:"min=0.0,max=1.0"`

	// The high watermark to stop refilling the pool, if zero none.
	RefillHighWatermark float64 `yaml:"high" validate:"min=0.0,max=1.0"`
}

WatermarkConfiguration contains watermark configuration for pools.

Jump to

Keyboard shortcuts

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