pools

package
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2026 License: Zlib, Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package pools provides functionality to manage and reuse resources like connections.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrClosed is returned if ResourcePool is used when it's closed.
	ErrClosed = errors.New("resource pool is closed")

	// ErrTimeout is returned if a resource get times out.
	ErrTimeout = errors.New("resource pool timed out")

	// ErrCtxTimeout is returned if a ctx is already expired by the time the resource pool is used
	ErrCtxTimeout = errors.New("resource pool context already expired")
)

Functions

This section is empty.

Types

type Factory

type Factory[T Resource] func(context.Context) (T, error)

Factory is a function that can be used to create a resource.

type Resource

type Resource interface {
	Close()
}

Resource defines the interface that every resource must provide. Thread synchronization between Close() and IsClosed() is the responsibility of the caller.

type ResourcePool

type ResourcePool[T Resource] struct {
	// contains filtered or unexported fields
}

ResourcePool allows you to use a pool of resources.

func NewResourcePool

func NewResourcePool[T Resource](prefillCtx context.Context, factory Factory[T], capacity, maxCap int, idleTimeout time.Duration, prefillParallelism int, logWait func(time.Time)) *ResourcePool[T]

NewResourcePool creates a new ResourcePool pool. capacity is the number of possible resources in the pool: there can be up to 'capacity' of these at a given time. maxCap specifies the extent to which the pool can be resized in the future through the SetCapacity function. You cannot resize the pool beyond maxCap. If a resource is unused beyond idleTimeout, it's replaced with a new one. An idleTimeout of 0 means that there is no timeout. A non-zero value of prefillParallelism causes the pool to be pre-filled. The value specifies how many resources can be opened in parallel.

func (*ResourcePool[T]) Active

func (rp *ResourcePool[T]) Active() int64

Active returns the number of active (i.e. non-nil) resources either in the pool or claimed for use

func (*ResourcePool[T]) Available

func (rp *ResourcePool[T]) Available() int64

Available returns the number of currently unused and available resources.

func (*ResourcePool[T]) Capacity

func (rp *ResourcePool[T]) Capacity() int64

Capacity returns the capacity.

func (*ResourcePool[T]) Close

func (rp *ResourcePool[T]) Close()

Close empties the pool calling Close on all its resources. You can call Close while there are outstanding resources. It waits for all resources to be returned (Put). After a Close, Get is not allowed.

func (*ResourcePool[T]) Exhausted

func (rp *ResourcePool[T]) Exhausted() int64

Exhausted returns the number of times Available dropped below 1

func (*ResourcePool[T]) Free added in v1.2.10

func (rp *ResourcePool[T]) Free(ctx context.Context)

func (*ResourcePool[T]) Get

func (rp *ResourcePool[T]) Get(ctx, soft context.Context) (resource T, err error)

Get will return the next available resource. If capacity has not been reached, it will create a new one using the factory. Otherwise, it will wait till the next resource becomes available or a timeout. A timeout of 0 is an indefinite wait.

func (*ResourcePool[T]) IdleClosed

func (rp *ResourcePool[T]) IdleClosed() int64

IdleClosed returns the count of resources closed due to idle timeout.

func (*ResourcePool[T]) IdleTimeout

func (rp *ResourcePool[T]) IdleTimeout() time.Duration

IdleTimeout returns the idle timeout.

func (*ResourcePool[T]) InUse

func (rp *ResourcePool[T]) InUse() int64

InUse returns the number of claimed resources from the pool

func (*ResourcePool[T]) IsClosed

func (rp *ResourcePool[T]) IsClosed() (closed bool)

IsClosed returns true if the resource pool is closed.

func (*ResourcePool[T]) MaxCap

func (rp *ResourcePool[T]) MaxCap() int64

MaxCap returns the max capacity.

func (*ResourcePool[T]) Put

func (rp *ResourcePool[T]) Put(resource T)

Put will return a resource to the pool. For every successful Get, a corresponding Put is required. If you no longer need a resource, you will need to call Put(nil) instead of returning the closed resource. This will cause a new resource to be created in its place.

func (*ResourcePool[T]) SetCapacity

func (rp *ResourcePool[T]) SetCapacity(capacity int) error

SetCapacity changes the capacity of the pool. You can use it to shrink or expand, but not beyond the max capacity. If the change requires the pool to be shrunk, SetCapacity waits till the necessary number of resources are returned to the pool. A SetCapacity of 0 is equivalent to closing the ResourcePool.

func (*ResourcePool[T]) SetIdleTimeout

func (rp *ResourcePool[T]) SetIdleTimeout(idleTimeout time.Duration)

SetIdleTimeout sets the idle timeout. It can only be used if there was an idle timeout set when the pool was created.

func (*ResourcePool[T]) StatsJSON

func (rp *ResourcePool[T]) StatsJSON() string

StatsJSON returns the stats in JSON format.

func (*ResourcePool[T]) WaitCount

func (rp *ResourcePool[T]) WaitCount() int64

WaitCount returns the total number of waits.

func (*ResourcePool[T]) WaitTime

func (rp *ResourcePool[T]) WaitTime() time.Duration

WaitTime returns the total wait time.

Directories

Path Synopsis
Package sync2 provides extra functionality along the same lines as sync.
Package sync2 provides extra functionality along the same lines as sync.
Package timer provides various enhanced timer functions.
Package timer provides various enhanced timer functions.

Jump to

Keyboard shortcuts

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