pool

package
v1.73.0-pre Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 19, 2024 License: BSD-3-Clause Imports: 3 Imported by: 0

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

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

func (p *Pool[V]) Add(item V) Handle[V]

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]) Clear

func (p *Pool[V]) Clear()

Clear removes all items from the pool.

func (*Pool[V]) Delete

func (p *Pool[V]) Delete(h Handle[V]) bool

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]) Len

func (p *Pool[V]) Len() int

Len returns the current size of the pool.

func (*Pool[V]) Peek

func (p *Pool[V]) Peek(h Handle[V]) (v V, ok bool)

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

func (p *Pool[V]) Take(h Handle[V]) (v V, ok bool)

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

func (p *Pool[V]) TakeRandom() (v V, ok bool)

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL