pool

package
v0.37.1 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2024 License: Apache-2.0 Imports: 3 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrPoolExhausted = errors.New("pool exhausted")

ErrPoolExhausted is returned if a pool cannot provide the requested slice.

Functions

This section is empty.

Types

type BucketedPool added in v0.37.0

type BucketedPool[T any] struct {
	// contains filtered or unexported fields
}

BucketedPool is a bucketed pool for variably sized T slices. It can be configured to not allow more than a maximum number of T items being used at a given time. Every slice obtained from the pool must be returned.

func MustNewBucketedPool added in v0.37.0

func MustNewBucketedPool[T any](minSize, maxSize int, factor float64, maxTotal uint64) *BucketedPool[T]

MustNewBucketedPool is like NewBucketedPool but panics if construction fails. Useful for package internal pools.

func NewBucketedPool added in v0.37.0

func NewBucketedPool[T any](minSize, maxSize int, factor float64, maxTotal uint64) (*BucketedPool[T], error)

NewBucketedPool returns a new BucketedPool with size buckets for minSize to maxSize increasing by the given factor and maximum number of used items. No more than maxTotal items can be used at any given time unless maxTotal is set to 0.

func (*BucketedPool[T]) Get added in v0.37.0

func (p *BucketedPool[T]) Get(sz int) (*[]T, error)

Get returns a slice into from the bucket that fits the given size.

func (*BucketedPool[T]) Put added in v0.37.0

func (p *BucketedPool[T]) Put(b *[]T)

Put returns a slice to the right bucket in the pool.

func (*BucketedPool[T]) UsedBytes added in v0.37.0

func (p *BucketedPool[T]) UsedBytes() uint64

type NoopPool added in v0.37.0

type NoopPool[T any] struct{}

NoopPool is pool that always allocated required slice on heap and ignore puts.

func (NoopPool[T]) Get added in v0.37.0

func (p NoopPool[T]) Get(sz int) (*[]T, error)

func (NoopPool[T]) Put added in v0.37.0

func (p NoopPool[T]) Put(*[]T)

type Pool added in v0.37.0

type Pool[T any] interface {
	// Get returns a new T slice that fits the given size.
	Get(sz int) (*[]T, error)
	// Put returns a T slice to the right bucket in the pool.
	Put(b *[]T)
}

Pool is a pool for slices of type T that can be reused.

type Work added in v0.35.0

type Work func()

Work is a unit of item to be worked on, like Java Runnable.

type WorkerPool added in v0.35.0

type WorkerPool interface {
	// Init initializes the worker pool.
	Init()

	// Go waits until the next worker becomes available and executes the given work.
	Go(work Work)

	// Close cancels all workers and waits for them to finish.
	Close()

	// Size returns the number of workers in the pool.
	Size() int
}

WorkerPool is a pool of goroutines that are reusable, similar to Java ThreadPool.

func NewWorkerPool added in v0.35.0

func NewWorkerPool(workers uint) WorkerPool

Jump to

Keyboard shortcuts

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