Documentation
¶
Overview ¶
Package pool provides functionality for pooling resources which require an open-and-close lifecycle (e.g. network connections).
Index ¶
- Variables
- type Resource
- type ResourceFactory
- type Resources
- func (p *Resources[R]) Close() error
- func (p *Resources[R]) Get(ctx context.Context) (*Resource[R], error)
- func (p *Resources[R]) SetMaxIdleResources(n int)
- func (p *Resources[R]) SetMaxTotalResources(n int)
- func (p *Resources[R]) SetResourceMaxIdleTime(d time.Duration)
- func (p *Resources[R]) SetResourceMaxLifetime(d time.Duration)
- func (p *Resources[R]) Shutdown(ctx context.Context) error
Constants ¶
This section is empty.
Variables ¶
var ErrPoolClosed error = errors.New("pool closed")
ErrPoolClosed indicates this pool instance has already been closed.
var ErrPoolClosing error = errors.New("pool closing")
ErrPoolClosing indicates this pool instance is currently shutting down and about to be closed.
var ErrPoolExhausted error = errors.New("pool exhausted")
ErrPoolExhausted indicates the maximum allowed number of resources for this pool instance is in use.
Functions ¶
This section is empty.
Types ¶
type Resource ¶
Resource represents a resource pool entry, as returned by Resources.Get.
func (*Resource[R]) Get ¶
func (r *Resource[R]) Get() R
Get retrieves the resource managed by this resource entry instance.
type ResourceFactory ¶
type ResourceFactory[R io.Closer] interface { // New creates a new resource instance. New(ctx context.Context) (R, error) }
ResourceFactory interface is used to create new resource instances on demand.
type Resources ¶
Resources provides resource pool functionality for the given resource type.
func NewResourcePool ¶
func NewResourcePool[R io.Closer](name string, factory ResourceFactory[R]) *Resources[R]
NewResourcePool creates a new resource pool using the given name and factory interface.
func (*Resources[R]) Get ¶
Get aquires a resource instance from the pool.
The requested resource is either taken from the existing idle queue or a new instance is created by invoking the factory interface given during pool creation. If a total resource limit has been set and the limit is reached, ErrPoolExhausted is returned.
func (*Resources[R]) SetMaxIdleResources ¶
SetMaxIdleResources sets the maximum number of idle resource instances contained in the pool.
If n <= 0, then there is no limit. The default value is 0 (no limit).
func (*Resources[R]) SetMaxTotalResources ¶
SetMaxTotalResources sets the maximum total number of resource instances contained in the pool.
If n <= 0, then there is no limit. The default value is 0 (no limit).
func (*Resources[R]) SetResourceMaxIdleTime ¶
SetResourceMaxIdleTime sets the timeout, after which an idle resource instance is closed.
If d <= 0, then there is no timeout. The default value is 0 (no timeout).
func (*Resources[R]) SetResourceMaxLifetime ¶
SetResourceMaxLifetime sets the timeout, after a resource instance is closed.
If d <= 0, then there is no timeout. The default value is 0 (no timeout).