Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Pool ¶
type Pool[T any] struct { // contains filtered or unexported fields }
A Pool is a set of temporary objects that may be individually saved and retrieved.
Any item stored in the Pool may be removed automatically at any time without notification. If the Pool holds the only reference when this happens, the item might be deallocated.
A Pool is safe for use by multiple goroutines simultaneously.
Pool's purpose is to cache allocated but unused items for later reuse, relieving pressure on the garbage collector. That is, it makes it easy to build efficient, thread-safe free lists. However, it is not suitable for all free lists.
An appropriate use of a Pool is to manage a group of temporary items silently shared among and potentially reused by concurrent independent clients of a package. Pool provides a way to amortize allocation overhead across many clients.
On the other hand, a free list maintained as part of a short-lived object is not a suitable use for a Pool, since the overhead does not amortize well in that scenario. It is more efficient to have such objects implement their own free list.
A Pool must not be copied after first use.
func (*Pool[T]) Borrow ¶
Borrow obtains an item from the pool. If the Max option is set, then this function will block until an item is returned back into the pool.
After the item is no longer required, you must call Return on the item.
func (*Pool[T]) Count ¶
Count returns approximately the number of items in the pool (idle and in-use). If you want an accurate number, call runtime.GC() twice before calling Count (not recommended).
func (*Pool[T]) ReturnItem ¶
func (p *Pool[T]) ReturnItem(item T)
ReturnItem returns an item back to the pool.
type PoolOption ¶
PoolOption configures the Pool
func WithBootstrapItems ¶
func WithBootstrapItems[T any](c int) PoolOption[T]
WithBootstrapItems creates an initial number of ready-to-use items in the pool
func WithSize ¶
func WithSize[T any](l int) PoolOption[T]
WithSize limits the number of items in the pool