Documentation
¶
Overview ¶
Package objswm provides a memory-optimized, fast, thread-safe object pool implementation.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ObjectPool ¶
type ObjectPool[T any] struct { // contains filtered or unexported fields }
ObjectPool is a memory-optimized, fast, thread-safe pool of objects.
func NewObjectPool ¶
func NewObjectPool[T any](factory Factory[T], config PoolConfig) (*ObjectPool[T], error)
NewObjectPool creates a new thread-safe object pool.
func (*ObjectPool[T]) Close ¶
func (p *ObjectPool[T]) Close()
Close shuts down the pool and discards all objects.
func (*ObjectPool[T]) Get ¶
func (p *ObjectPool[T]) Get(ctx context.Context) (T, error)
Get retrieves an object from the pool. If the pool is empty:
- If AllowBlock is true, it waits until an object is available or BlockTimeout is reached
- If AllowBlock is false, it creates a new object if below MaxSize, or returns an error
func (*ObjectPool[T]) IdleCount ¶
func (p *ObjectPool[T]) IdleCount() int
IdleCount returns the number of idle objects in the pool.
func (*ObjectPool[T]) Put ¶
func (p *ObjectPool[T]) Put(obj T)
Put returns an object to the pool. If the pool is full or closed, the object may be discarded.
func (*ObjectPool[T]) Size ¶
func (p *ObjectPool[T]) Size() int
Size returns the current total size of the pool (in use + idle).
type PoolConfig ¶
type PoolConfig struct {
// InitialSize is the number of objects to pre-create when the pool is initialized.
InitialSize int
// MaxSize is the maximum number of objects the pool can hold.
// If set to 0, the pool has no size limit.
MaxSize int
// MaxIdle is the maximum number of idle objects to keep in the pool.
// If set to 0, all idle objects are kept until MaxSize is reached.
MaxIdle int
// TTL is the time-to-live for an idle object before it's removed from the pool.
// If set to 0, objects don't expire.
TTL time.Duration
// AllowBlock determines if Get() should block when the pool is empty.
// If false, Get() will return an error when no objects are available.
AllowBlock bool
// BlockTimeout is the maximum time to wait for an object to become available.
// If set to 0 and AllowBlock is true, Get() will wait indefinitely.
BlockTimeout time.Duration
}
PoolConfig contains configuration options for the ObjectPool.
func DefaultPoolConfig ¶
func DefaultPoolConfig() PoolConfig
DefaultPoolConfig returns a default configuration for ObjectPool.