Documentation ¶
Overview ¶
Package pools provides functionality to manage and reuse resources like connections.
Index ¶
- Variables
- type Factory
- type IDPool
- type Numbered
- func (nu *Numbered) Get(id int64, purpose string) (val interface{}, err error)
- func (nu *Numbered) GetAll() (vals []interface{})
- func (nu *Numbered) GetIdle(timeout time.Duration, purpose string) (vals []interface{})
- func (nu *Numbered) GetOutdated(age time.Duration, purpose string) (vals []interface{})
- func (nu *Numbered) Put(id int64)
- func (nu *Numbered) Register(id int64, val interface{}, enforceTimeout bool) error
- func (nu *Numbered) Size() (size int64)
- func (nu *Numbered) StatsJSON() string
- func (nu *Numbered) Unregister(id int64, reason string)
- func (nu *Numbered) WaitForEmpty()
- type Resource
- type ResourcePool
- func (rp *ResourcePool) Active() int64
- func (rp *ResourcePool) Available() int64
- func (rp *ResourcePool) Capacity() int64
- func (rp *ResourcePool) Close()
- func (rp *ResourcePool) Get(ctx context.Context) (resource Resource, err error)
- func (rp *ResourcePool) IdleClosed() int64
- func (rp *ResourcePool) IdleTimeout() time.Duration
- func (rp *ResourcePool) InUse() int64
- func (rp *ResourcePool) IsClosed() (closed bool)
- func (rp *ResourcePool) MaxCap() int64
- func (rp *ResourcePool) Put(resource Resource)
- func (rp *ResourcePool) SetCapacity(capacity int) error
- func (rp *ResourcePool) SetIdleTimeout(idleTimeout time.Duration)
- func (rp *ResourcePool) StatsJSON() string
- func (rp *ResourcePool) WaitCount() int64
- func (rp *ResourcePool) WaitTime() time.Duration
Constants ¶
This section is empty.
Variables ¶
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") )
Functions ¶
This section is empty.
Types ¶
type IDPool ¶
IDPool is used to ensure that the set of IDs in use concurrently never contains any duplicates. The IDs start at 1 and increase without bound, but will never be larger than the peak number of concurrent uses.
IDPool's Get() and Set() methods can be used concurrently.
type Numbered ¶
type Numbered struct {
// contains filtered or unexported fields
}
Numbered allows you to manage resources by tracking them with numbers. There are no interface restrictions on what you can track.
func NewNumbered ¶
func NewNumbered() *Numbered
func (*Numbered) Get ¶
Get locks the resource for use. It accepts a purpose as a string. If it cannot be found, it returns a "not found" error. If in use, it returns a "in use: purpose" error.
func (*Numbered) GetAll ¶
func (nu *Numbered) GetAll() (vals []interface{})
GetAll returns the list of all resources in the pool.
func (*Numbered) GetIdle ¶
GetIdle returns a list of resurces that have been idle for longer than timeout, and locks them. It does not return any resources that are already locked.
func (*Numbered) GetOutdated ¶
GetOutdated returns a list of resources that are older than age, and locks them. It does not return any resources that are already locked.
func (*Numbered) Register ¶
Register starts tracking a resource by the supplied id. It does not lock the object. It returns an error if the id already exists.
func (*Numbered) Unregister ¶
Unregiester forgets the specified resource. If the resource is not present, it's ignored.
func (*Numbered) WaitForEmpty ¶
func (nu *Numbered) WaitForEmpty()
WaitForEmpty returns as soon as the pool becomes empty
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 struct {
// contains filtered or unexported fields
}
ResourcePool allows you to use a pool of resources.
func NewResourcePool ¶
func NewResourcePool(factory Factory, capacity, maxCap int, idleTimeout time.Duration) *ResourcePool
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 discarded. An idleTimeout of 0 means that there is no timeout.
func (*ResourcePool) Active ¶
func (rp *ResourcePool) Active() int64
Active returns the number of active (i.e. non-nil) resources either in the pool or claimed for use
func (*ResourcePool) Available ¶
func (rp *ResourcePool) Available() int64
Available returns the number of currently unused and available resources.
func (*ResourcePool) Capacity ¶
func (rp *ResourcePool) Capacity() int64
Capacity returns the capacity.
func (*ResourcePool) Close ¶
func (rp *ResourcePool) 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) Get ¶
func (rp *ResourcePool) Get(ctx context.Context) (resource Resource, 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) IdleClosed ¶
func (rp *ResourcePool) IdleClosed() int64
IdleClosed returns the count of resources closed due to idle timeout.
func (*ResourcePool) IdleTimeout ¶
func (rp *ResourcePool) IdleTimeout() time.Duration
IdleTimeout returns the idle timeout.
func (*ResourcePool) InUse ¶
func (rp *ResourcePool) InUse() int64
InUse returns the number of claimed resources from the pool
func (*ResourcePool) IsClosed ¶
func (rp *ResourcePool) IsClosed() (closed bool)
IsClosed returns true if the resource pool is closed.
func (*ResourcePool) MaxCap ¶
func (rp *ResourcePool) MaxCap() int64
MaxCap returns the max capacity.
func (*ResourcePool) Put ¶
func (rp *ResourcePool) Put(resource Resource)
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. The will eventually cause a new resource to be created in its place.
func (*ResourcePool) SetCapacity ¶
func (rp *ResourcePool) 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) SetIdleTimeout ¶
func (rp *ResourcePool) 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) StatsJSON ¶
func (rp *ResourcePool) StatsJSON() string
StatsJSON returns the stats in JSON format.
func (*ResourcePool) WaitCount ¶
func (rp *ResourcePool) WaitCount() int64
WaitCount returns the total number of waits.
func (*ResourcePool) WaitTime ¶
func (rp *ResourcePool) WaitTime() time.Duration
WaitTime returns the total wait time.