Help us shape the future of Go! Take the 2026 Go Developer Survey

resourcepool

package
v0.1.56 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2026 License: AGPL-3.0 Imports: 1 Imported by: 0

Documentation

Overview

Package resourcepool provides a small, reusable, homogeneous collection type with an attached (opaque) policy.

This package is intentionally "dumb": - It enforces homogeneity (all items share the same key). - It supports safe removal (Take) for single-use allocation patterns. - It does not evaluate policy or perform IO; higher-level code does that.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Keyed

type Keyed[K comparable] interface {
	PoolKey() K
}

Keyed is implemented by items that belong to a homogeneous pool.

type Pool

type Pool[K comparable, R Keyed[K], P any] struct {
	Name   string `json:"name" yaml:"name"`
	Key    K      `json:"key" yaml:"key"`
	Policy P      `json:"policy" yaml:"policy"`

	Items []R `json:"items,omitempty" yaml:"items,omitempty"`
}

Pool is a homogeneous collection of items identified by a key, plus an attached policy of caller-defined type.

Policy is opaque to this package: Pool carries it, but does not interpret it.

func New

func New[K comparable, R Keyed[K], P any](name string, key K, policy P) Pool[K, R, P]

func (*Pool[K, R, P]) Add

func (p *Pool[K, R, P]) Add(item R) error

Add appends item if it satisfies the pool's homogeneity invariant.

func (*Pool[K, R, P]) AddMany

func (p *Pool[K, R, P]) AddMany(items []R) error

AddMany appends items if all satisfy the pool's homogeneity invariant.

func (*Pool[K, R, P]) Take

func (p *Pool[K, R, P]) Take(n int, pred func(R) bool) ([]R, error)

Take removes up to n items matching pred, returning exactly n items or an error.

On insufficient matches, the pool is left unchanged.

func (Pool[K, R, P]) Validate

func (p Pool[K, R, P]) Validate() error

Validate checks that the pool is internally consistent.

Jump to

Keyboard shortcuts

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