Documentation ¶
Overview ¶
Package pool contains a generic type for managing a pool of resources; for example, connections to a database, or to a remote service.
Unlike sync.Pool from the Go standard library, this pool does not remove items from the pool when garbage collection happens, nor is it safe for concurrent use like sync.Pool.
Index ¶
- type Handle
- type Pool
- func (p *Pool[V]) Add(item V) Handle[V]
- func (p *Pool[V]) AppendTakeAll(dst []V) []V
- func (p *Pool[V]) Clear()
- func (p *Pool[V]) Delete(h Handle[V]) bool
- func (p *Pool[V]) Len() int
- func (p *Pool[V]) Peek(h Handle[V]) (v V, ok bool)
- func (p *Pool[V]) Take(h Handle[V]) (v V, ok bool)
- func (p *Pool[V]) TakeRandom() (v V, ok bool)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Handle ¶
type Handle[V any] struct { // contains filtered or unexported fields }
Handle is an opaque handle to a resource in a pool. It is used to delete an item from the pool, without requiring the item to be comparable.
type Pool ¶
type Pool[V any] struct { // contains filtered or unexported fields }
Pool is a pool of resources. It is not safe for concurrent use.
func (*Pool[V]) Add ¶
Add adds an item to the pool and returns a handle to it. The handle can be used to delete the item from the pool with the Delete method.
func (*Pool[V]) AppendTakeAll ¶
func (p *Pool[V]) AppendTakeAll(dst []V) []V
AppendTakeAll removes all items from the pool, appending them to the provided slice (which can be nil) and returning them. The returned slice can be nil if the provided slice was nil and the pool was empty.
This function does not free the backing storage for the pool; to do that, use the Clear function.
func (*Pool[V]) Delete ¶
Delete removes the item from the pool.
It reports whether the element was deleted; it will return false if the item has been taken with the TakeRandom function, or if the item was already deleted.
func (*Pool[V]) Peek ¶
Peek will return the item with the given handle without removing it from the pool.
It will return ok=false if the item has been deleted or previously taken.
func (*Pool[V]) Take ¶
Take will remove the item with the given handle from the pool and return it.
It will return ok=false and the zero value if the item has been deleted or previously taken.
func (*Pool[V]) TakeRandom ¶
TakeRandom returns and removes a random element from p and reports whether there was one to take.
It will return ok=false and the zero value if the pool is empty.