pool

package
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: May 3, 2025 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewPoolConfigBuilder

func NewPoolConfigBuilder() *poolConfigBuilder

NewPoolConfigBuilder creates a new pool configuration builder with default settings. It initializes all configuration parameters with safe default values and sets up the necessary parameter relationships. Returns a builder ready for customization.

Types

type AggressivenessLevel

type AggressivenessLevel int
const (
	AggressivenessDisabled       AggressivenessLevel = 0
	AggressivenessConservative   AggressivenessLevel = 1
	AggressivenessBalanced       AggressivenessLevel = 2
	AggressivenessAggressive     AggressivenessLevel = 3
	AggressivenessVeryAggressive AggressivenessLevel = 4
	AggressivenessExtreme        AggressivenessLevel = 5
)

type Pool

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

Pool is a generic object pool implementation that provides efficient object reuse. It uses a two-level caching strategy with a fast path (L1 cache) and a main pool. The pool is designed to be thread-safe and supports dynamic resizing based on usage patterns.

Key features: - Two-level caching (L1 cache + main pool) for optimal performance - Dynamic resizing based on usage patterns - Thread-safe operations - Configurable growth and shrink behavior - Optional object cleanup - Detailed statistics tracking

Type parameter T must be a pointer type. Non-pointer types will cause an error.

func NewPool

func NewPool[T any](config *PoolConfig, allocator func() T, cleaner func(T)) (*Pool[T], error)

NewPool creates a new object pool with the given configuration, allocator, cleaner, and pool type. Only pointers can be stored in the pool. The allocator function creates new objects, and the cleaner function resets objects before they are reused.

func (*Pool[T]) Close

func (p *Pool[T]) Close() error

Close closes the pool and releases all resources. If there are outstanding objects, it will wait for them to be returned before closing.

func (*Pool[T]) Get

func (p *Pool[T]) Get() (zero T, err error)

Get returns an object from the pool, either from L1 cache or the ring buffer, preferring L1.

func (*Pool[T]) GetBlockedReaders

func (p *Pool[T]) GetBlockedReaders() int

GetBlockedReaders returns the number of readers currently blocked waiting for objects

func (*Pool[T]) GetPoolStatsSnapshot

func (p *Pool[T]) GetPoolStatsSnapshot() *PoolStatsSnapshot

GetPoolStatsSnapshot returns a snapshot of the current pool statistics

func (*Pool[T]) IdleCheck

func (p *Pool[T]) IdleCheck(idles *int, shrinkPermissionIdleness *bool)

IdleCheck determines if the pool has been idle long enough to consider shrinking. It updates the idle count and shrink permission based on the configured thresholds.

func (*Pool[T]) IsGrowth

func (p *Pool[T]) IsGrowth() bool

func (*Pool[T]) IsShrunk

func (p *Pool[T]) IsShrunk() bool

func (*Pool[T]) PrintPoolStats

func (p *Pool[T]) PrintPoolStats()

PrintPoolStats prints the current statistics of the pool to stdout. This includes information about pool capacity, object usage, hit rates, and performance metrics.

func (*Pool[T]) Put

func (p *Pool[T]) Put(obj T) error

Put returns an object to the pool. The object will be cleaned using the cleaner function before being made available for reuse.

func (*Pool[T]) RingBufferCapacity

func (p *Pool[T]) RingBufferCapacity() int

func (*Pool[T]) RingBufferLength

func (p *Pool[T]) RingBufferLength() int

func (*Pool[T]) SlowPath

func (p *Pool[T]) SlowPath() (obj T, err error)

SlowPath retrieves an object from the ring buffer. It blocks if the ring buffer is empty and the ring buffer is in blocking mode. We always try to refill the ring buffer before calling the slow path.

type PoolConfig

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

PoolConfig defines the configuration parameters for the pool. It controls various aspects of pool behavior including growth, shrinking, and performance characteristics.

func (*PoolConfig) GetFastPath

func (c *PoolConfig) GetFastPath() *fastPathParameters

func (*PoolConfig) GetGrowth

func (c *PoolConfig) GetGrowth() *growthParameters

func (*PoolConfig) GetHardLimit

func (c *PoolConfig) GetHardLimit() int

func (*PoolConfig) GetInitialCapacity

func (c *PoolConfig) GetInitialCapacity() int

Getter methods for PoolConfig

func (*PoolConfig) GetRingBufferConfig

func (c *PoolConfig) GetRingBufferConfig() *config.RingBufferConfig

func (*PoolConfig) GetShrink

func (c *PoolConfig) GetShrink() *shrinkParameters

func (*PoolConfig) IsVerbose

func (c *PoolConfig) IsVerbose() bool

type PoolStatsSnapshot

type PoolStatsSnapshot struct {
	// Basic Pool Stats
	ObjectsInUse       uint64
	AvailableObjects   uint64
	CurrentCapacity    uint64
	RingBufferLength   uint64
	PeakInUse          uint64
	TotalGets          uint64
	TotalGrowthEvents  uint64
	TotalShrinkEvents  uint64
	ConsecutiveShrinks int32

	// Fast Path Resize Stats
	LastResizeAtGrowthNum uint64
	CurrentL1Capacity     uint64
	L1Length              uint64

	// Fast Get Stats
	L1HitCount  uint64
	L2HitCount  uint64
	L3MissCount uint64

	// Fast Return Stats
	FastReturnHit  uint64
	FastReturnMiss uint64
	L2SpillRate    float64

	// Usage Stats
	RequestPerObject float64
	Utilization      float64

	// Time Stats
	LastGetTime    time.Time
	LastShrinkTime time.Time
	LastGrowTime   time.Time
}

PoolStatsSnapshot represents a snapshot of the pool's statistics at a given moment

func (*PoolStatsSnapshot) Validate

func (s *PoolStatsSnapshot) Validate(reqNum int) error

Jump to

Keyboard shortcuts

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