pool

package
v0.0.3-beta.9 Latest Latest
Warning

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

Go to latest
Published: Jan 27, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BlockingPool

type BlockingPool[T any] interface {
	Pool[T]
	// GetContext waits to retrieve an object until ctx is cancelled or times out.
	GetContext(ctx context.Context) (T, bool)
}

BlockingPool extends the Pool interface with context-aware blocking acquisition.

type ChanPool

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

ChanPool is a bounded object pool based on channels, suitable for scenarios requiring resource limits. Unlike SyncPool, ChanPool has an explicit capacity limit and objects are not subject to GC.

func NewChanPool

func NewChanPool[T any](size int, opts ...Option[T]) *ChanPool[T]

NewChanPool creates a bounded object pool based on channels.

func (*ChanPool[T]) Cap

func (cp *ChanPool[T]) Cap() int

Cap returns the capacity of the pool.

func (*ChanPool[T]) Close

func (cp *ChanPool[T]) Close()

Close closes the pool and calls closeFn on all remaining objects. After calling Close, Get and GetContext will return zero values.

func (*ChanPool[T]) Get

func (cp *ChanPool[T]) Get() T

Get retrieves an object from the pool without blocking.

func (*ChanPool[T]) GetContext

func (cp *ChanPool[T]) GetContext(ctx context.Context) (T, bool)

GetContext waits to retrieve an object until ctx is cancelled or times out. If allowCreate is true and newFn is set, creates a new object when pool is empty. If allowCreate is false, always waits for an object from the pool.

func (*ChanPool[T]) IsClosed

func (cp *ChanPool[T]) IsClosed() bool

IsClosed returns whether the pool has been closed.

func (*ChanPool[T]) Len

func (cp *ChanPool[T]) Len() int

Len returns the number of objects currently cached in the pool.

func (*ChanPool[T]) Put

func (cp *ChanPool[T]) Put(obj T) bool

Put attempts to return an object to the pool.

type Option

type Option[T any] func(*Options[T])

Option is a function that configures Options.

func WithAccept

func WithAccept[T any](accept func(T) bool) Option[T]

func WithAllowCreate

func WithAllowCreate[T any](allow bool) Option[T]

func WithClose

func WithClose[T any](close func(T)) Option[T]

func WithNew

func WithNew[T any](newFunc func() T) Option[T]

func WithReset

func WithReset[T any](reset func(T) T) Option[T]

WithReset sets the reset function for pool objects.

type Options

type Options[T any] struct {
	New         func() T
	Reset       func(T) T
	Accept      func(T) bool
	Close       func(T)
	AllowCreate bool // For ChanPool.GetContext: if true, create new object when pool is empty
}

Options contains configuration functions for pool behavior.

type Pool

type Pool[T any] interface {
	// Get retrieves an object from the pool.
	Get() T
	// Put attempts to return an object to the pool.
	Put(T) bool
}

Pool defines the basic interface for a generic object pool.

type SyncPool

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

SyncPool is a generic object pool based on sync.Pool, suitable for frequent allocation of small objects.

func NewSyncPool

func NewSyncPool[T any](opts ...Option[T]) *SyncPool[T]

NewSyncPool creates an object pool based on sync.Pool.

func (*SyncPool[T]) Get

func (sp *SyncPool[T]) Get() T

Get returns an object from the pool or creates a new one via the New function.

func (*SyncPool[T]) Put

func (sp *SyncPool[T]) Put(obj T) bool

Put returns an object to the pool.

Jump to

Keyboard shortcuts

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