pool

package
v0.1.39 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2026 License: Apache-2.0 Imports: 1 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Linkable

type Linkable[T any] interface {
	Next() T
	SetNext(T)
}

Linkable defines the requirements for an item to be managed by the Pool. T represents the pointer to the item (e.g., *MyStruct).

type Pool

type Pool[E any, P interface {
	*E
	Linkable[P]
}] struct {

	// New optionally specifies a function to generate
	// a value when Get would otherwise return nil.
	// It may not be changed concurrently with calls to Get.
	New func() P
	// contains filtered or unexported fields
}

Pool is a lock-free (per-P) pool of objects. Objects in the pool never expire.

The objects must implement the Linkable interface (with a pointer receiver.

Example:

type MyObject struct {
	ID   int
	next *MyObject
}

func (o *MyObject) Next() *MyObject        { return o.next }
func (o *MyObject) SetNext(next *MyObject) { o.next = next }

Synchronization is done using runtime.procPin/runtime.procUnpin.

It take two opera types: E: The struct type (e.g., UserObject) P: The pointer type (e.g., *UserObject)

The constraint `interface { *E; Linkable[P] }` enforces that: 1. P is exactly a pointer to E (*E) 2. P implements Linkable for itself

func New

func New[E any, P interface {
	*E
	Linkable[P]
}](newFunc func() P) *Pool[E, P]

New creates a new Pool.

func (*Pool[E, P]) Get

func (p *Pool[E, P]) Get() P

Get retrieves an object from the pool. If the pool is empty, it allocates a new one using New function or zero value.

func (*Pool[E, P]) Put

func (p *Pool[E, P]) Put(node P)

Put returns an object to the pool.

Jump to

Keyboard shortcuts

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