pool

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: May 6, 2025 License: MIT Imports: 11 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

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 (*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 PoolConfigBuilder

type PoolConfigBuilder interface {
	// bulk methods
	//
	// SetPoolBasicConfigs sets the basic configuration parameters for the pool.
	// Parameters:
	//   - initialCapacity: Initial size of both ring buffer and fast path (can be overridden)
	//   - hardLimit: Maximum number of objects the pool can grow to
	//   - verbose: Enable detailed logging of pool operations
	//   - enableChannelGrowth: Enable dynamic growth of the fast path channel
	//   - enableStats: Enable collection of non-essential pool statistics
	//
	// Note: Zero or negative values are ignored, default values will be used instead.
	SetPoolBasicConfigs(initialCapacity int, hardLimit int, verbose, enableChannelGrowth, enableStats bool) PoolConfigBuilder

	// SetRingBufferBasicConfigs sets the basic configuration parameters for the ring buffer.
	// Parameters:
	//   - block: Whether operations should block when buffer is full/empty
	//   - rTimeout: Read operation timeout
	//   - wTimeout: Write operation timeout
	//   - bothTimeout: Sets both read and write timeouts to the same value
	//
	// Note: Timeout values must be positive to take effect.
	SetRingBufferBasicConfigs(block bool, rTimeout, wTimeout, bothTimeout time.Duration) PoolConfigBuilder

	// SetRingBufferGrowthConfigs sets the growth configuration parameters for the ring buffer.
	// Parameters:
	//   - exponentialThresholdFactor: Threshold for switching from exponential to fixed growth
	//   - growthPercent: Percentage growth rate when below threshold
	//   - fixedGrowthFactor: Fixed step size for growth when above threshold
	//
	// Note: Zero or negative values are ignored, default values will be used instead.
	SetRingBufferGrowthConfigs(exponentialThresholdFactor float64, growthPercent float64, fixedGrowthFactor float64) PoolConfigBuilder

	// SetRingBufferShrinkConfigs sets custom shrink parameters for the ring buffer.
	// Parameters:
	//   - checkInterval: Time between shrink eligibility checks
	//   - idleThreshold: Minimum idle duration before shrinking
	//   - shrinkCooldown: Minimum time between shrink operations
	//   - minIdleBeforeShrink: Required consecutive idle checks
	//   - stableUnderutilizationRounds: Required stable underutilization rounds
	//   - minCapacity: Minimum capacity after shrinking
	//   - maxConsecutiveShrinks: Maximum consecutive shrink operations
	//   - minUtilizationBeforeShrink: Utilization threshold for shrinking
	//   - shrinkPercent: Percentage by which to shrink
	//
	// Note: Zero or negative values are ignored, default values will be used instead.
	SetRingBufferShrinkConfigs(checkInterval, idleThreshold, shrinkCooldown time.Duration, minIdleBeforeShrink, stableUnderutilizationRounds, minCapacity, maxConsecutiveShrinks int, minUtilizationBeforeShrink, shrinkPercent float64) PoolConfigBuilder

	// EnforceCustomConfig disables default shrink configuration, requiring manual setting
	// of all shrink parameters. This is useful when you need precise control over
	// the shrinking behavior and don't want to use the preset aggressiveness levels.
	EnforceCustomConfig() PoolConfigBuilder

	// SetShrinkAggressiveness sets the auto-shrink level (1-5) with preset defaults for both
	// ring buffer and fast path. Each level represents a different balance between
	// memory efficiency and performance.
	//
	// Levels:
	//
	//	1: Conservative - Minimal shrinking, prioritizes performance
	//	2: Moderate - Balanced approach
	//	3: Aggressive - More aggressive shrinking
	//	4: Very Aggressive - Heavy shrinking
	//	5: Extreme - Maximum shrinking
	//
	// Returns an error if:
	//   - Custom configuration is enforced
	//   - Level is out of valid range
	SetShrinkAggressiveness(level AggressivenessLevel) (PoolConfigBuilder, error)

	// SetFastPathBasicConfigs sets the basic configuration parameters for the fast path (L1 cache).
	// Parameters:
	//   - initialSize: Initial capacity of the fast path buffer
	//   - growthEventsTrigger: Number of growth events before fast path grows
	//   - shrinkEventsTrigger: Number of shrink events before fast path shrinks
	//   - fillAggressiveness: How aggressively to fill the fast path initially
	//   - refillPercent: Threshold for refilling the fast path
	SetFastPathBasicConfigs(initialSize int, growthEventsTrigger int, shrinkEventsTrigger int, fillAggressiveness, refillPercent float64) PoolConfigBuilder

	// SetFastPathGrowthConfigs sets the growth configuration parameters for the fast path.
	// Parameters:
	//   - exponentialThresholdFactor: Threshold for switching growth modes
	//   - fixedGrowthFactor: Fixed step size for growth above threshold
	//   - growthPercent: Percentage growth rate below threshold
	SetFastPathGrowthConfigs(exponentialThresholdFactor float64, fixedGrowthFactor float64, growthPercent float64) PoolConfigBuilder

	// SetFastPathShrinkConfigs sets the shrink configuration parameters for the fast path.
	// Parameters:
	//   - shrinkPercent: Percentage by which to shrink the fast path
	//   - minCapacity: Minimum capacity after shrinking
	SetFastPathShrinkConfigs(shrinkPercent float64, minCapacity int) PoolConfigBuilder

	// SetFastPathShrinkAggressiveness sets the shrink aggressiveness level for the fast path.
	// Uses the same aggressiveness levels as the main pool (1-5).
	// Panics if:
	//   - Custom configuration is enforced
	//   - Level is out of valid range
	SetFastPathShrinkAggressiveness(level AggressivenessLevel) PoolConfigBuilder

	SetInitialCapacity(cap int) PoolConfigBuilder
	SetHardLimit(count int) PoolConfigBuilder
	SetVerbose(verbose bool) PoolConfigBuilder
	SetGrowthExponentialThresholdFactor(factor float64) PoolConfigBuilder
	SetGrowthPercent(percent float64) PoolConfigBuilder
	SetFixedGrowthFactor(factor float64) PoolConfigBuilder
	SetShrinkCheckInterval(interval time.Duration) PoolConfigBuilder
	SetIdleThreshold(duration time.Duration) PoolConfigBuilder
	SetMinIdleBeforeShrink(count int) PoolConfigBuilder
	SetShrinkCooldown(duration time.Duration) PoolConfigBuilder
	SetMinUtilizationBeforeShrink(threshold float64) PoolConfigBuilder
	SetStableUnderutilizationRounds(rounds int) PoolConfigBuilder
	SetShrinkPercent(percent float64) PoolConfigBuilder
	SetMinShrinkCapacity(minCap int) PoolConfigBuilder
	SetMaxConsecutiveShrinks(count int) PoolConfigBuilder
	SetFastPathInitialSize(count int) PoolConfigBuilder
	SetFastPathFillAggressiveness(percent float64) PoolConfigBuilder
	SetFastPathRefillPercent(percent float64) PoolConfigBuilder
	SetFastPathEnableChannelGrowth(enable bool) PoolConfigBuilder
	SetFastPathGrowthEventsTrigger(count int) PoolConfigBuilder
	SetFastPathGrowthPercent(percent float64) PoolConfigBuilder
	SetFastPathExponentialThresholdFactor(percent float64) PoolConfigBuilder
	SetFastPathFixedGrowthFactor(percent float64) PoolConfigBuilder
	SetFastPathShrinkEventsTrigger(count int) PoolConfigBuilder
	SetFastPathShrinkPercent(percent float64) PoolConfigBuilder
	SetFastPathShrinkMinCapacity(minCap int) PoolConfigBuilder
	SetPreReadBlockHookAttempts(attempts int) PoolConfigBuilder
	SetRingBufferBlocking(block bool) PoolConfigBuilder
	SetRingBufferTimeout(d time.Duration) PoolConfigBuilder
	SetRingBufferReadTimeout(d time.Duration) PoolConfigBuilder
	SetRingBufferWriteTimeout(d time.Duration) PoolConfigBuilder
	SetEnableStats(enable bool) PoolConfigBuilder
	Build() (*PoolConfig, error)
}

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.

type PoolObj

type PoolObj[T any] interface {
	Get() (T, error)
	Put(T) error
	Close() error
	PrintPoolStats()
}

func NewPool

func NewPool[T any](config *PoolConfig, allocator func() T, cleaner func(T)) (PoolObj[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.

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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