Documentation ¶
Overview ¶
Package pools provides functionality to manage and reuse resources like connections.
Index ¶
- Variables
- type Factory
- type Resource
- type ResourcePool
- func (rp *ResourcePool) Available() int64
- func (rp *ResourcePool) Capacity() int64
- func (rp *ResourcePool) Close()
- func (rp *ResourcePool) Get() (resource Resource, err error)
- func (rp *ResourcePool) IdleTimeout() time.Duration
- 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) Stats() (capacity, available, maxCap, waitCount int64, ...)
- func (rp *ResourcePool) StatsJSON() string
- func (rp *ResourcePool) TryGet() (resource Resource, err error)
- func (rp *ResourcePool) WaitCount() int64
- func (rp *ResourcePool) WaitTime() time.Duration
Constants ¶
This section is empty.
Variables ¶
var (
CLOSED_ERR = fmt.Errorf("ResourcePool is closed")
)
Functions ¶
This section is empty.
Types ¶
type Resource ¶
type Resource interface {
Close()
}
Every resource needs to suport the Resource interface. Thread synchronization between Close() and IsClosed() is the responsibility 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 initial capacity of the pool. maxCap is the maximum capacity. If a resource is unused beyond idleTimeout, it's discarded. An idleTimeout of 0 means that there is no timeout.
func (*ResourcePool) Available ¶
func (rp *ResourcePool) Available() int64
func (*ResourcePool) Capacity ¶
func (rp *ResourcePool) Capacity() int64
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 and TryGet are not allowed.
func (*ResourcePool) Get ¶
func (rp *ResourcePool) Get() (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 indefinitely wait till the next resource becomes available.
func (*ResourcePool) IdleTimeout ¶
func (rp *ResourcePool) IdleTimeout() time.Duration
func (*ResourcePool) IsClosed ¶
func (rp *ResourcePool) IsClosed() (closed bool)
func (*ResourcePool) MaxCap ¶
func (rp *ResourcePool) MaxCap() int64
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)
func (*ResourcePool) Stats ¶
func (rp *ResourcePool) Stats() (capacity, available, maxCap, waitCount int64, waitTime, idleTimeout time.Duration)
func (*ResourcePool) StatsJSON ¶
func (rp *ResourcePool) StatsJSON() string
func (*ResourcePool) TryGet ¶
func (rp *ResourcePool) TryGet() (resource Resource, err error)
TryGet will return the next available resource. If none is available, and capacity has not been reached, it will create a new one using the factory. Otherwise, it will return nil with no error.
func (*ResourcePool) WaitCount ¶
func (rp *ResourcePool) WaitCount() int64
func (*ResourcePool) WaitTime ¶
func (rp *ResourcePool) WaitTime() time.Duration