randalloc

package
v0.22.0 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RandAllocator

type RandAllocator[T comparable] struct {
	// contains filtered or unexported fields
}

RandAllocator hands out items such that, at any moment, no two callers hold the same item concurrently. Call Acquire() to get exclusive use of an item, and Release() when done.

If all items are busy, Acquire() will block until one is released or the context is canceled.

func NewRandAllocator

func NewRandAllocator[T comparable](vals sets.Set[T]) *RandAllocator[T]

NewRandAllocator constructs a RandAllocator[T] from a set of items.

func (*RandAllocator[T]) Acquire

func (ra *RandAllocator[T]) Acquire(ctx context.Context) (T, error)

Acquire returns an item that is not currently in use by any other caller. It randomizes selection among the currently-free items. If none are free, it waits until one is released or ctx is canceled.

func (*RandAllocator[T]) Release

func (ra *RandAllocator[T]) Release(item T)

Release marks an item as free again and wakes any waiters.

func (*RandAllocator[T]) Replace

func (ra *RandAllocator[T]) Replace(vals sets.Set[T])

Replace atomically swaps the candidate item set and wakes all waiters. Any items currently in use may remain absent from the new item set; they simply won't be handed out again once released.

func (*RandAllocator[T]) SetPreference

func (ra *RandAllocator[T]) SetPreference(order []T)

SetPreference installs a ranked order for future Acquires: free items are handed out in this order, with items absent from the ranking falling back to random selection after the ranked ones. The ranking may contain items not (or no longer) in the candidate set; those entries are ignored. Wakes waiters so a blocked Acquire re-evaluates with the new order.

Jump to

Keyboard shortcuts

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