Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ItemWrap ¶
type ItemWrap struct { Item interface{} // contains filtered or unexported fields }
ItemWrap wraps the item returned by the pool's factory.
func (*ItemWrap) MarkAsInvalid ¶
func (iw *ItemWrap) MarkAsInvalid()
MarkAsInvalid marks the item as invalid (eg. unusable, unstable or broken) so that after it gets put back in the pool, it is discarded. It will eventually get garbage collected.
type Options ¶
type Options struct { // Initial creates an initial number of ready-to-use items in the pool. Initial int // Max sets the maximum number of items kept in the pool. Max *int // DisableCount, when set, disables the pool's Count function. // Only set this if you need a runtime Finalizer for the item returned // by the factory (alternatively, wrap your item in another struct, with the Finalizer // added to the original item). DisableCount bool }
Options configures the Pool struct.
type Pool ¶
type Pool 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 New ¶
func New(opts ...interface{}) Pool
New creates a new Pool. opts accepts either an int (representing the max) or an Options struct.
func (*Pool) 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) 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) ReturnItem ¶
ReturnItem returns an item back to the pool. Usually this function is never called, as the recommended approach is to call Return on the item.
func (*Pool) SetFactory ¶
func (p *Pool) SetFactory(factory func() interface{})
SetFactory specifies a function to generate an item when Borrow is called. It must not be called concurrently with calls to Borrow.
NOTE: factory should generally only return pointer types, since a pointer can be put into the return interface value without an allocation.