threadsafe

package module
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2025 License: MIT Imports: 5 Imported by: 1

README

threadsafe Go Documentation MIT License

The threadsafe package provides thread-safe operations for various data structures common in concurrent Go applications.

The interfaces provided by the package are generic, and attempt to be quite exhaustive feature wise. If a more minimal interface would be better aligned for your application, create it as needed.

All interface implementations in this package are thread-safe and can be used concurrently.

Key Features

  • Generic, thread-safe maps, sets, queues, heaps, and priority queues.
  • Iterator-first APIs for idiomatic range loops.
    • Note: due to the snapshotting used to keep the iterators thread-safe, some iterators may be less performant than a standard Range iteration.
  • Multiple concurrency strategies (mutex, RWMutex, sync.Map) so you can pick the right trade-offs.

Tests and benchmarks

The package provides a Makefile with targets for running tests and benchmarks:

make test
make bench

Contribute

For contributions, please open a GitHub issue with your questions and suggestions. Before submitting an issue, have a look at the existing TODO list to see if your idea is already in the works.

Documentation

Overview

Package threadsafe implements thread-safe operations.

Package threadsafe implements thread-safe operations.

Package threadsafe implements thread-safe operations.

Package threadsafe implements thread-safe operations.

Package threadsafe implements thread-safe operations.

Package threadsafe implements thread-safe operations.

Package threadsafe implements thread-safe operations.

Package threadsafe implements thread-safe operations.

Package threadsafe implements thread-safe operations.

Package threadsafe implements thread-safe operations.

Package threadsafe implements thread-safe operations.

Package threadsafe implements thread-safe operations.

Package threadsafe implements thread-safe operations.

Package threadsafe implements thread-safe operations.

Package threadsafe implements thread-safe operations.

Package threadsafe implements thread-safe operations.

Package threadsafe implements thread-safe operations.

Package threadsafe implements thread-safe operations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CorePriorityQueue added in v0.4.0

type CorePriorityQueue[T any] struct {
	// contains filtered or unexported fields
}

CorePriorityQueue is a thread-safe priority queue that implements the core PriorityQueue interface. It does not expose any indexed mutation helpers, nor onSwap callbacks.

It is a generic min-heap parameterized by a less comparator. The zero value is not ready; construct via NewCorePriorityQueue. The less(a,b) comparator must define a strict weak ordering (irreflexive, transitive, consistent).

Complexity: Push/Pop O(log n), Peek O(1); Range does not mutate the heap.

func NewCorePriorityQueue added in v0.4.0

func NewCorePriorityQueue[T any](less func(a, b T) bool) *CorePriorityQueue[T]

NewCorePriorityQueue creates a new minimal priority queue using the given comparator.

func (*CorePriorityQueue[T]) All added in v0.6.0

func (q *CorePriorityQueue[T]) All() iter.Seq[T]

All returns an iterator over items in the queue in internal heap order (not sorted). The iteration order is implementation-defined and not guaranteed to be priority-sorted.

func (*CorePriorityQueue[T]) Clear added in v0.4.0

func (q *CorePriorityQueue[T]) Clear()

Clear removes all items.

func (*CorePriorityQueue[T]) Len added in v0.4.0

func (q *CorePriorityQueue[T]) Len() int

Len returns the number of items.

func (*CorePriorityQueue[T]) Peek added in v0.4.0

func (q *CorePriorityQueue[T]) Peek() (item T, ok bool)

Peek returns the minimum item without removing it.

func (*CorePriorityQueue[T]) Pop added in v0.4.0

func (q *CorePriorityQueue[T]) Pop() (item T, ok bool)

Pop removes and returns the minimum item per the comparator.

func (*CorePriorityQueue[T]) Push added in v0.4.0

func (q *CorePriorityQueue[T]) Push(items ...T)

Push inserts one or more items into the queue.

func (*CorePriorityQueue[T]) Range added in v0.4.0

func (q *CorePriorityQueue[T]) Range(f func(item T) bool)

Range iterates over a snapshot of items in arbitrary internal order. Mutations during range does not affect the current iteration.

type Heap added in v0.4.1

type Heap[T any] interface {
	// Push adds one or more items to the heap.
	Push(items ...T)

	// Pop removes and returns the top-priority item.
	// If the heap is empty, it returns ok == false and the zero value of T.
	Pop() (item T, ok bool)

	// Peek returns the top-priority item without removing it.
	// If the heap is empty, it returns ok == false and the zero value of T.
	Peek() (item T, ok bool)

	// Len returns the current number of items stored in the heap.
	Len() int

	// Clear removes all items from the heap.
	Clear()

	// Slice returns a copy of the current heap contents in internal heap order
	// (not sorted). The returned slice is safe to read but may be stale if
	// new items are added concurrently.
	Slice() []T

	// Range calls f sequentially for each item present in the heap in internal
	// heap order. If f returns false, Range stops the iteration early.
	Range(f func(item T) bool)

	// All returns an iterator over items in the heap in internal heap order (not sorted).
	// The iteration order is implementation-defined and not guaranteed to be priority-sorted.
	//
	// Example usage:
	//
	//	for item := range myHeap.All() {
	//	    fmt.Println(item)
	//	}
	All() iter.Seq[T]
}

Heap is a generic binary heap interface for any type T. Ordering is defined by the implementation, typically via a provided less function. All operations are expected to be safe for concurrent use by multiple goroutines.

type IndexedPriorityQueue added in v0.4.0

type IndexedPriorityQueue[T any] struct {
	// contains filtered or unexported fields
}

IndexedPriorityQueue is a thread-safe binary min-heap implementation parameterized by a Less comparator. It optionally notifies a caller-supplied onSwap callback whenever two indices swap, which can be used to maintain external index fields.

The zero value is not ready to use; construct via NewIndexedPriorityQueue. The less(a,b) comparator must define a strict weak ordering (irreflexive, transitive, consistent).

Semantics mirror container/heap where applicable; indices are stable only for the lifetime between operations that may move elements. For external index maintenance (e.g., storing an "index" field inside elements), implementations may provide a swap-callback to notify callers when indices change. Note that index values refer to internal heap storage and are unstable across operations.

Complexity: Push/Pop/Fix/RemoveAt O(log n), Peek O(1); Range does not mutate the heap.

func NewIndexedPriorityQueue added in v0.4.0

func NewIndexedPriorityQueue[T any](
	less func(a, b T) bool,
	onSwap func(i, j int, items []T),
) *IndexedPriorityQueue[T]

NewIndexedPriorityQueue creates a new heap with the provided comparator. less(a,b) should return true when a has higher priority than b (i.e., a comes before b). onSwap is optional; if non-nil it's called under the write lock whenever two items swap indices and as such must not block or call back into the queue.

func (*IndexedPriorityQueue[T]) All added in v0.6.0

func (q *IndexedPriorityQueue[T]) All() iter.Seq[T]

All returns an iterator over items in the queue in internal heap order (not sorted). The iteration order is implementation-defined and not guaranteed to be priority-sorted.

func (*IndexedPriorityQueue[T]) Clear added in v0.4.0

func (q *IndexedPriorityQueue[T]) Clear()

Clear removes all items.

func (*IndexedPriorityQueue[T]) Fix added in v0.4.0

func (q *IndexedPriorityQueue[T]) Fix(i int)

Fix restores heap order after the item at index i may have changed.

func (*IndexedPriorityQueue[T]) Len added in v0.4.0

func (q *IndexedPriorityQueue[T]) Len() int

Len returns number of items.

func (*IndexedPriorityQueue[T]) Peek added in v0.4.0

func (q *IndexedPriorityQueue[T]) Peek() (item T, ok bool)

Peek returns the minimum item without removing it.

func (*IndexedPriorityQueue[T]) Pop added in v0.4.0

func (q *IndexedPriorityQueue[T]) Pop() (item T, ok bool)

Pop removes and returns the minimum item.

func (*IndexedPriorityQueue[T]) Push added in v0.4.0

func (q *IndexedPriorityQueue[T]) Push(items ...T)

Push inserts one or more items into the heap.

func (*IndexedPriorityQueue[T]) Range added in v0.4.0

func (q *IndexedPriorityQueue[T]) Range(f func(item T) bool)

Range iterates over the current snapshot in arbitrary order. Mutations during range does not affect the current iteration.

func (*IndexedPriorityQueue[T]) RemoveAt added in v0.4.0

func (q *IndexedPriorityQueue[T]) RemoveAt(i int) (item T, ok bool)

RemoveAt removes and returns the item at index i, if valid.

func (*IndexedPriorityQueue[T]) UpdateAt added in v0.4.0

func (q *IndexedPriorityQueue[T]) UpdateAt(i int, x T) bool

UpdateAt replaces the element at index i and restores invariants.

type Map

type Map[K comparable, V any] interface {
	// Get retrieves the value for the given key.
	Get(key K) (value V, loaded bool)
	// Set stores a value for the given key.
	Set(key K, value V)
	// Delete removes the key from the map. If the key doesn't exist, Delete is a no-op.
	Delete(key K)
	// Len returns the number of items in the map.
	Len() int
	// Clear removes all items from the map.
	Clear()

	// CompareAndSwap executes the compare-and-swap operation for a key.
	CompareAndSwap(key K, oldValue, newValue V) bool
	// LoadAndDelete deletes the value for a key, returning the previous value if any.
	LoadAndDelete(key K) (previous V, loaded bool)
	// LoadOrStore returns the existing value for the key if present. Otherwise, it stores and
	// returns the given value. The loaded result is true if the value was loaded, false if stored.
	LoadOrStore(key K, value V) (previous V, loaded bool)
	// Swap swaps the value for a key and returns the previous value if any.
	Swap(key K, value V) (previous V, loaded bool)

	// GetAll returns all key-value pairs in the map.
	GetAll() map[K]V
	// GetMany retrieves select key-value pairs.
	GetMany(keys []K) map[K]V
	// SetMany sets multiple key-value pairs.
	SetMany(entries map[K]V)

	// Equals reports whether the logical content of this map and the other map is the same.
	// Requires an equal function since V is not of type comparable.
	Equals(other Map[K, V], equalFn func(a, b V) bool) bool

	// Range calls f sequentially for each key and value present in the map.
	// If f returns false, range stops the iteration.
	Range(f func(key K, value V) bool)

	// All returns an iterator over key-value pairs in the map.
	// The iteration order is not guaranteed to be consistent.
	// Note: for mutex backed maps this snapshots before iteration, making Range more performant.
	All() iter.Seq2[K, V]
	// Keys returns an iterator over keys in the map.
	// The iteration order is not guaranteed to be consistent.
	// Note: for mutex backed maps this snapshots before iteration, making Range more performant.
	Keys() iter.Seq[K]
	// Values returns an iterator over values in the map.
	// The iteration order is not guaranteed to be consistent.
	// Note: for mutex backed maps this snapshots before iteration, making Range more performant.
	Values() iter.Seq[V]
}

Map is a generic interface for stores with any type V. It allows concurrent appends and atomic flushes.

type MapDiff

type MapDiff[K comparable, V any] struct {
	AddedOrModified map[K]V
	Removed         map[K]V
}

MapDiff represents the difference between two maps.

func CalculateMapDiff

func CalculateMapDiff[K comparable, V any](
	newData, oldData map[K]V,
	equalFunc func(V, V) bool,
) MapDiff[K, V]

CalculateMapDiff calculates the difference between two maps. It returns a MapDiff containing the added or modified entries and the removed entries.

type MutexMap added in v0.2.0

type MutexMap[K comparable, V any] struct {
	// contains filtered or unexported fields
}

MutexMap is a thread-safe implementation of Map using sync.Mutex.

func MutexMapFromMap added in v0.4.0

func MutexMapFromMap[K comparable, V any](m map[K]V, equalFn func(V, V) bool) *MutexMap[K, V]

MutexMapFromMap creates a new instance of MutexMap from values in the provided map.

func NewMutexMap added in v0.2.0

func NewMutexMap[K comparable, V any](equalFn func(V, V) bool) *MutexMap[K, V]

NewMutexMap creates a new instance of MutexMap.

func (*MutexMap[K, V]) All added in v0.6.0

func (m *MutexMap[K, V]) All() iter.Seq2[K, V]

All returns an iterator over key-value pairs in the map. The iteration order is not guaranteed to be consistent. Note: since this snapshots before iteration, Range is more performant.

func (*MutexMap[K, V]) Clear added in v0.2.0

func (m *MutexMap[K, V]) Clear()

Clear removes all items from the map.

func (*MutexMap[K, V]) CompareAndSwap added in v0.2.0

func (m *MutexMap[K, V]) CompareAndSwap(key K, oldValue, newValue V) bool

CompareAndSwap executes the compare-and-swap operation for a key. The MutexMap must have been initialized with an equal function, lest this function panics.

func (*MutexMap[K, V]) Delete added in v0.2.0

func (m *MutexMap[K, V]) Delete(key K)

Delete removes the key from the map.

func (*MutexMap[K, V]) Equals added in v0.3.2

func (m *MutexMap[K, V]) Equals(other Map[K, V], equalFn func(a, b V) bool) bool

Equals reports whether the logical content of this map and the other map is the same. Requires equalFn to be provided to decide how two values of type V are compared.

func (*MutexMap[K, V]) Get added in v0.2.0

func (m *MutexMap[K, V]) Get(key K) (V, bool)

Get retrieves the value for the given key.

func (*MutexMap[K, V]) GetAll added in v0.2.0

func (m *MutexMap[K, V]) GetAll() map[K]V

GetAll returns a copy of all key-value pairs in the map.

func (*MutexMap[K, V]) GetMany added in v0.2.0

func (m *MutexMap[K, V]) GetMany(keys []K) map[K]V

GetMany retrieves multiple keys at once.

func (*MutexMap[K, V]) Keys added in v0.6.0

func (m *MutexMap[K, V]) Keys() iter.Seq[K]

Keys returns an iterator over keys in the map. The iteration order is not guaranteed to be consistent. Note: since this snapshots before iteration, Range is more performant.

func (*MutexMap[K, V]) Len added in v0.2.0

func (m *MutexMap[K, V]) Len() int

Len returns the number of items in the map.

func (*MutexMap[K, V]) LoadAndDelete added in v0.5.0

func (m *MutexMap[K, V]) LoadAndDelete(key K) (V, bool)

LoadAndDelete deletes the value for a key, returning the previous value if any.

func (*MutexMap[K, V]) LoadOrStore added in v0.5.0

func (m *MutexMap[K, V]) LoadOrStore(key K, value V) (V, bool)

LoadOrStore returns the existing value for the key if present. Otherwise, it stores and returns the given value. The loaded result is true if the value was loaded, false if stored.

func (*MutexMap[K, V]) Range added in v0.2.0

func (m *MutexMap[K, V]) Range(f func(key K, value V) bool)

Range calls f sequentially for each key and value present in the map. If f returns false, range stops the iteration.

func (*MutexMap[K, V]) Set added in v0.2.0

func (m *MutexMap[K, V]) Set(key K, value V)

Set stores a value for the given key.

func (*MutexMap[K, V]) SetMany added in v0.2.0

func (m *MutexMap[K, V]) SetMany(entries map[K]V)

SetMany sets multiple key-value pairs at once.

func (*MutexMap[K, V]) Swap added in v0.2.0

func (m *MutexMap[K, V]) Swap(key K, value V) (V, bool)

Swap swaps the value for a key and returns the previous value if any.

func (*MutexMap[K, V]) Values added in v0.6.0

func (m *MutexMap[K, V]) Values() iter.Seq[V]

Values returns an iterator over values in the map. The iteration order is not guaranteed to be consistent. Note: since this snapshots before iteration, Range is more performant.

type MutexSlice

type MutexSlice[T any] struct {
	// contains filtered or unexported fields
}

MutexSlice is a thread-safe buffer for any type T, featuring concurrent appends and atomic flushes.

func MutexSliceFromSlice added in v0.4.0

func MutexSliceFromSlice[T any](slice []T) *MutexSlice[T]

MutexSliceFromSlice creates a new MutexSlice from a standard slice.

func NewMutexSlice

func NewMutexSlice[T any](initialCap int) *MutexSlice[T]

NewMutexSlice creates a new MutexSlice with an optional initial capacity.

func (*MutexSlice[T]) All added in v0.6.0

func (s *MutexSlice[T]) All() iter.Seq[T]

All returns an iterator over all items in the slice. The iteration order is not guaranteed to be consistent.

func (*MutexSlice[T]) Append

func (s *MutexSlice[T]) Append(item ...T)

Append appends items to the slice in a thread-safe way.

func (*MutexSlice[T]) Flush

func (s *MutexSlice[T]) Flush() []T

Flush atomically retrieves all items and clears the slice. Returns a slice with the previous contents.

func (*MutexSlice[T]) Len

func (s *MutexSlice[T]) Len() int

Len returns the current number of items in the slice.

func (*MutexSlice[T]) Peek

func (s *MutexSlice[T]) Peek() []T

Peek returns a copy of the current slice contents without clearing. The returned slice is safe to read but may be stale if new items are added concurrently.

type PriorityQueue added in v0.4.0

type PriorityQueue[T any] interface {
	// Push inserts one or more items into the queue.
	Push(items ...T)

	// Pop removes and returns the minimum item per the comparator.
	// If empty, returns ok == false and the zero value of T.
	Pop() (item T, ok bool)

	// Peek returns the current minimum without removing it.
	// If empty, returns ok == false and the zero value of T.
	Peek() (item T, ok bool)

	// Len returns the number of items in the queue.
	Len() int

	// Clear removes all items from the queue.
	Clear()

	// Range iterates over items in arbitrary internal order. Returning false stops early.
	Range(f func(item T) bool)

	// All returns an iterator over items in the queue in internal heap order (not sorted).
	// The iteration order is implementation-defined and not guaranteed to be priority-sorted.
	//
	// Example usage:
	//
	//	for item := range myPQ.All() {
	//	    fmt.Println(item)
	//	}
	All() iter.Seq[T]
}

PriorityQueue is a generic thread-safe priority queue interface (min-heap) for any type T. Ordering is defined by the implementation at construction time via a comparator. Implementations of this interface are expected to be safe for parallel use in multiple goroutines.

type PriorityQueueIndexed added in v0.4.0

type PriorityQueueIndexed[T any] interface {
	PriorityQueue[T]

	// Fix re-establishes queue ordering after the item at index i may have changed.
	// Safe no-op if i is out of range.
	Fix(i int)

	// RemoveAt removes and returns the item at index i in the queue's internal array.
	// If i is out of range, returns zero value and ok == false.
	RemoveAt(i int) (item T, ok bool)

	// UpdateAt replaces the element at index i with x and restores queue invariants.
	// If i is out of range, it is a no-op and returns false.
	UpdateAt(i int, x T) bool
}

PriorityQueueIndexed exposes index-based mutation helpers intended for advanced use-cases.

As the index-based helpers brings on mutation risks it's important to note:

  • If callers mutate ordering fields of an element already in the queue, they MUST call Fix or UpdateAt to restore queue invariants.
  • Peek returns a copy of T (or the pointer value for pointer T). Callers must avoid in-place mutations without Fix/UpdateAt.

type Queue added in v0.4.0

type Queue[T any] interface {
	// Push adds one or more items to the back of the queue.
	Push(items ...T)

	// Pop removes and returns the item at the front of the queue.
	// If the queue is empty, it returns ok == false and the zero value of T.
	Pop() (item T, ok bool)

	// Peek returns the item at the front of the queue without removing it.
	// If the queue is empty, it returns ok == false and the zero value of T.
	Peek() (item T, ok bool)

	// Len returns the current number of items stored in the queue.
	Len() int

	// Clear removes all items from the queue.
	Clear()

	// Slice returns a copy of the current queue contents from front to back.
	// The returned slice is safe to read but may be stale if new items are added
	// concurrently.
	Slice() []T

	// Range calls f sequentially for each item present in the queue from front
	// to back. If f returns false, Range stops the iteration early.
	Range(f func(item T) bool)

	// All returns an iterator over items in the queue from front to back.
	// The iteration order matches the queue order (FIFO).
	//
	// Example usage:
	//
	//	for item := range myQueue.All() {
	//	    fmt.Println(item)
	//	}
	All() iter.Seq[T]
}

Queue is a generic FIFO queue interface for any type T. All operations must be safe for concurrent use by multiple goroutines.

The contract is intentionally similar in style to Set and Map interfaces in this repository to provide a consistent developer experience.

type RWMutexHeap added in v0.4.1

type RWMutexHeap[T any] struct {
	// contains filtered or unexported fields
}

RWMutexHeap is a thread-safe binary heap implementation protected by a sync.RWMutex. The ordering is determined by the provided less function: less(a, b) == true means a has higher priority than b (i.e., a comes out before b). This makes it a min-heap when less(a, b) is a < b, and a max-heap when less(a, b) is a > b.

The zero value is not ready to use; use NewRWMutexHeap to construct with a comparator.

func NewRWMutexHeap added in v0.4.1

func NewRWMutexHeap[T any](less func(a, b T) bool) *RWMutexHeap[T]

NewRWMutexHeap creates a new RWMutexHeap with the provided less function.

func (*RWMutexHeap[T]) All added in v0.6.0

func (h *RWMutexHeap[T]) All() iter.Seq[T]

All returns an iterator over items in the heap in internal heap order (not sorted). The iteration order is implementation-defined and not guaranteed to be priority-sorted.

func (*RWMutexHeap[T]) Clear added in v0.4.1

func (h *RWMutexHeap[T]) Clear()

Clear removes all items from the heap.

func (*RWMutexHeap[T]) Len added in v0.4.1

func (h *RWMutexHeap[T]) Len() int

Len returns the current number of items.

func (*RWMutexHeap[T]) Peek added in v0.4.1

func (h *RWMutexHeap[T]) Peek() (item T, ok bool)

Peek returns the top-priority item without removing it.

func (*RWMutexHeap[T]) Pop added in v0.4.1

func (h *RWMutexHeap[T]) Pop() (item T, ok bool)

Pop removes and returns the top-priority item. If the heap is empty it returns ok == false and the zero value of T.

func (*RWMutexHeap[T]) Push added in v0.4.1

func (h *RWMutexHeap[T]) Push(items ...T)

Push adds one or more items to the heap.

func (*RWMutexHeap[T]) Range added in v0.4.1

func (h *RWMutexHeap[T]) Range(f func(item T) bool)

Range calls f sequentially for each item in internal heap order. This action does not modify the heap or its items.

func (*RWMutexHeap[T]) Slice added in v0.4.1

func (h *RWMutexHeap[T]) Slice() []T

Slice returns a copy of the heap contents in internal heap order.

type RWMutexMap added in v0.3.0

type RWMutexMap[K comparable, V any] struct {
	// contains filtered or unexported fields
}

RWMutexMap is a thread-safe implementation of Map using sync.RWMutex.

func NewRWMutexMap added in v0.3.0

func NewRWMutexMap[K comparable, V any](equalFn func(V, V) bool) *RWMutexMap[K, V]

NewRWMutexMap creates a new instance of RWMutexMap.

func RWMutexMapFromMap added in v0.4.0

func RWMutexMapFromMap[K comparable, V any](m map[K]V, equalFn func(V, V) bool) *RWMutexMap[K, V]

RWMutexMapFromMap creates a new instance of RWMutexMap from values in the provided map.

func (*RWMutexMap[K, V]) All added in v0.6.0

func (m *RWMutexMap[K, V]) All() iter.Seq2[K, V]

All returns an iterator over key-value pairs in the map. The iteration order is not guaranteed to be consistent. Note: since this snapshots before iteration, Range is more performant.

func (*RWMutexMap[K, V]) Clear added in v0.3.0

func (m *RWMutexMap[K, V]) Clear()

Clear removes all items from the map.

func (*RWMutexMap[K, V]) CompareAndSwap added in v0.3.0

func (m *RWMutexMap[K, V]) CompareAndSwap(key K, oldValue, newValue V) bool

CompareAndSwap executes the compare-and-swap operation for a key. The RWMutexMap must have been initialized with an equal function, lest this function panics.

func (*RWMutexMap[K, V]) Delete added in v0.3.0

func (m *RWMutexMap[K, V]) Delete(key K)

Delete removes the key from the map.

func (*RWMutexMap[K, V]) Equals added in v0.3.2

func (m *RWMutexMap[K, V]) Equals(other Map[K, V], equalFn func(a, b V) bool) bool

Equals reports whether the logical content of this map and the other map is the same. Requires equalFn to be provided to decide how two values of type V are compared.

func (*RWMutexMap[K, V]) Get added in v0.3.0

func (m *RWMutexMap[K, V]) Get(key K) (V, bool)

Get retrieves the value for the given key.

func (*RWMutexMap[K, V]) GetAll added in v0.3.0

func (m *RWMutexMap[K, V]) GetAll() map[K]V

GetAll returns a copy of all key-value pairs in the map.

func (*RWMutexMap[K, V]) GetMany added in v0.3.0

func (m *RWMutexMap[K, V]) GetMany(keys []K) map[K]V

GetMany retrieves multiple keys at once.

func (*RWMutexMap[K, V]) Keys added in v0.6.0

func (m *RWMutexMap[K, V]) Keys() iter.Seq[K]

Keys returns an iterator over keys in the map. The iteration order is not guaranteed to be consistent. Note: since this snapshots before iteration, Range is more performant.

func (*RWMutexMap[K, V]) Len added in v0.3.0

func (m *RWMutexMap[K, V]) Len() int

Len returns the number of items in the map.

func (*RWMutexMap[K, V]) LoadAndDelete added in v0.5.0

func (m *RWMutexMap[K, V]) LoadAndDelete(key K) (V, bool)

LoadAndDelete deletes the value for a key, returning the previous value if any.

func (*RWMutexMap[K, V]) LoadOrStore added in v0.5.0

func (m *RWMutexMap[K, V]) LoadOrStore(key K, value V) (V, bool)

LoadOrStore returns the existing value for the key if present. Otherwise, it stores and returns the given value. The loaded result is true if the value was loaded, false if stored.

func (*RWMutexMap[K, V]) Range added in v0.3.0

func (m *RWMutexMap[K, V]) Range(f func(key K, value V) bool)

Range calls f sequentially for each key and value present in the map. If f returns false, range stops the iteration.

func (*RWMutexMap[K, V]) Set added in v0.3.0

func (m *RWMutexMap[K, V]) Set(key K, value V)

Set stores a value for the given key.

func (*RWMutexMap[K, V]) SetMany added in v0.3.0

func (m *RWMutexMap[K, V]) SetMany(entries map[K]V)

SetMany sets multiple key-value pairs at once.

func (*RWMutexMap[K, V]) Swap added in v0.3.0

func (m *RWMutexMap[K, V]) Swap(key K, value V) (V, bool)

Swap swaps the value for a key and returns the previous value if any.

func (*RWMutexMap[K, V]) Values added in v0.6.0

func (m *RWMutexMap[K, V]) Values() iter.Seq[V]

Values returns an iterator over values in the map. The iteration order is not guaranteed to be consistent. Note: since this snapshots before iteration, Range is more performant.

type RWMutexQueue added in v0.4.0

type RWMutexQueue[T any] struct {
	// contains filtered or unexported fields
}

RWMutexQueue is a thread-safe FIFO queue implementation backed by a slice and protected by a sync.RWMutex.

The implementation aims for amortized O(1) Push and Pop by keeping a head index instead of shifting the slice on every Pop. When the internal slice has too much unused prefix, it is resliced to reclaim memory.

The zero value of RWMutexQueue is ready to use.

func NewRWMutexQueue added in v0.4.0

func NewRWMutexQueue[T any]() *RWMutexQueue[T]

NewRWMutexQueue creates a new instance of RWMutexQueue.

func (*RWMutexQueue[T]) All added in v0.6.0

func (q *RWMutexQueue[T]) All() iter.Seq[T]

All returns an iterator over items in the queue from front to back. The iteration order matches the queue order (FIFO).

func (*RWMutexQueue[T]) Clear added in v0.4.0

func (q *RWMutexQueue[T]) Clear()

Clear removes all items from the queue.

func (*RWMutexQueue[T]) Len added in v0.4.0

func (q *RWMutexQueue[T]) Len() int

Len returns the current number of items.

func (*RWMutexQueue[T]) Peek added in v0.4.0

func (q *RWMutexQueue[T]) Peek() (item T, ok bool)

Peek returns the item at the front without removing it.

func (*RWMutexQueue[T]) Pop added in v0.4.0

func (q *RWMutexQueue[T]) Pop() (item T, ok bool)

Pop removes and returns the item at the front of the queue. If the queue is empty it returns ok == false and the zero value of T.

func (*RWMutexQueue[T]) Push added in v0.4.0

func (q *RWMutexQueue[T]) Push(items ...T)

Push adds one or more items to the back of the queue.

func (*RWMutexQueue[T]) Range added in v0.4.0

func (q *RWMutexQueue[T]) Range(f func(item T) bool)

Range calls f sequentially for each item from front to back. This action does not modify the queue or its items.

func (*RWMutexQueue[T]) Slice added in v0.4.0

func (q *RWMutexQueue[T]) Slice() []T

Slice returns a copy of the queue contents from front to back.

type RWMutexSet added in v0.4.0

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

RWMutexSet is a thread-safe implementation of Set using sync.RWMutex.

func NewRWMutexSet added in v0.4.0

func NewRWMutexSet[T comparable]() *RWMutexSet[T]

NewRWMutexSet creates a new instance of RWMutexSet.

func (*RWMutexSet[T]) Add added in v0.4.0

func (s *RWMutexSet[T]) Add(item T) (added bool)

Add stores an item in the set.

func (*RWMutexSet[T]) All added in v0.6.0

func (s *RWMutexSet[T]) All() iter.Seq[T]

All returns an iterator over all items in the set. The iteration order is not guaranteed to be consistent.

func (*RWMutexSet[T]) Clear added in v0.4.0

func (s *RWMutexSet[T]) Clear()

Clear removes all items from the set.

func (*RWMutexSet[T]) Delete added in v0.5.0

func (s *RWMutexSet[T]) Delete(item T) (removed bool)

Delete removes an item from the set.

func (*RWMutexSet[T]) Has added in v0.4.0

func (s *RWMutexSet[T]) Has(item T) bool

Has returns true if the item is in the set, otherwise false.

func (*RWMutexSet[T]) Len added in v0.4.0

func (s *RWMutexSet[T]) Len() int

Len returns the number of items in the set.

func (*RWMutexSet[T]) Range added in v0.4.0

func (s *RWMutexSet[T]) Range(f func(item T) bool)

Range calls f sequentially for each item present in the set. If f returns false, range stops the iteration.

func (*RWMutexSet[T]) Slice added in v0.4.0

func (s *RWMutexSet[T]) Slice() []T

Slice returns a copy of the set as a slice.

type RWMutexSlice added in v0.4.0

type RWMutexSlice[T any] struct {
	// contains filtered or unexported fields
}

RWMutexSlice is a thread-safe buffer for any type T, featuring concurrent appends and atomic flushes.

func NewRWMutexSlice added in v0.4.0

func NewRWMutexSlice[T any](initialCap int) *RWMutexSlice[T]

NewRWMutexSlice creates a new RWMutexSlice with an optional initial capacity.

func RWMutexSliceFromSlice added in v0.4.0

func RWMutexSliceFromSlice[T any](slice []T) *RWMutexSlice[T]

RWMutexSliceFromSlice creates a new RWMutexSlice from a slice.

func (*RWMutexSlice[T]) All added in v0.6.0

func (s *RWMutexSlice[T]) All() iter.Seq[T]

All returns an iterator over all items in the slice. The iteration order is not guaranteed to be consistent.

func (*RWMutexSlice[T]) Append added in v0.4.0

func (s *RWMutexSlice[T]) Append(item ...T)

Append appends items to the slice.

func (*RWMutexSlice[T]) Flush added in v0.4.0

func (s *RWMutexSlice[T]) Flush() []T

Flush atomically retrieves all items and clears the slice. Returns a slice with the previous contents.

func (*RWMutexSlice[T]) Len added in v0.4.0

func (s *RWMutexSlice[T]) Len() int

Len returns the current number of items in the slice.

func (*RWMutexSlice[T]) Peek added in v0.4.0

func (s *RWMutexSlice[T]) Peek() []T

Peek returns a copy of the current slice contents without clearing. The returned slice is safe to read but may be stale if new items are added concurrently.

type Set added in v0.4.0

type Set[T comparable] interface {
	// Add stores an item in the set.
	Add(item T) (added bool)
	// Delete removes an item from the set. Returns true if the item was present and removed,
	// false if it was not in the set. If the item doesn't exist, Delete is a no-op.
	Delete(item T) (removed bool)
	// Has returns true if the item is in the set, otherwise false.
	Has(item T) bool
	// Len returns the number of items in the set.
	Len() int
	// Clear removes all items from the set.
	Clear()
	// Slice returns a copy of the set as a slice.
	Slice() []T
	// Range calls f sequentially for each item present in the set.
	// If f returns false, range stops the iteration.
	Range(f func(item T) bool)

	// All returns an iterator over all items in the set.
	// The iteration order is not guaranteed to be consistent.
	// Note: for mutex backed sets this snapshots before iteration, making Range more performant.
	All() iter.Seq[T]
}

Set is a generic interface for a set store of any type T.

type ShardedSlice added in v0.4.1

type ShardedSlice[T any] struct {
	// contains filtered or unexported fields
}

ShardedSlice is a high-throughput thread-safe buffer that splits its storage into several independent shards. Each shard is a full Slice implementation, so operations on different shards proceed in parallel with zero contention.

Using a ShardedSlice will reduce lock contention, retain atomicity at the API level, and allow for simple fall-back to a single mutex-backed slice.

The slice returned by Flush() / Peek() concatenates shards in ascending index order. The order of items within each shard is preserved, but the overall order is only guaranteed per-shard, which is usually acceptable for buffer/queue-like workloads where ordering across goroutines is not critical.

All methods are wait-free with bounded work and require no global locks.

The zero value defaults to a single shard for compatibility, though NewShardedSlice should be used for performance-sensitive use cases to configure the optimal shard count.

func NewShardedSlice added in v0.4.1

func NewShardedSlice[T any](shardCount, initialCap int) *ShardedSlice[T]

NewShardedSlice creates a ShardedSlice with the given number of shards. Each shard is pre-allocated with initialCap capacity. shardCount must be >0; if <=0, it is coerced to 1.

func (*ShardedSlice[T]) All added in v0.6.0

func (s *ShardedSlice[T]) All() iter.Seq[T]

All returns an iterator over all items in the slice. The iteration order is not guaranteed to be consistent.

func (*ShardedSlice[T]) Append added in v0.4.1

func (s *ShardedSlice[T]) Append(item ...T)

Append adds the items to one of the shards, selected in a round-robin manner using an atomic counter. This ensures good key distribution without requiring hashing the items themselves.

func (*ShardedSlice[T]) Flush added in v0.4.1

func (s *ShardedSlice[T]) Flush() []T

Flush atomically retrieves and clears all shards, concatenating the results into a single slice.

func (*ShardedSlice[T]) Len added in v0.4.1

func (s *ShardedSlice[T]) Len() int

Len returns the combined length of all shards.

func (*ShardedSlice[T]) Peek added in v0.4.1

func (s *ShardedSlice[T]) Peek() []T

Peek returns a copy of the current contents of all shards without clearing them.

type Slice

type Slice[T any] interface {
	// Append appends an item to the buffer in a thread-safe way.
	Append(item ...T)
	// Len returns the current number of items in the buffer.
	Len() int
	// Peek returns a copy of the current buffer contents without clearing.
	// The returned slice is safe to read but may be stale if new items are added concurrently.
	Peek() []T
	// Flush atomically retrieves all items and clears the buffer.
	// Returns a slice with the previous contents.
	Flush() []T

	// All returns an iterator over all items in the slice in order.
	//
	// Example usage:
	//
	//	for item := range mySlice.All() {
	//	    fmt.Println(item)
	//	}
	All() iter.Seq[T]
}

Slice is a generic interface for stores with any type T.

type SyncMap

type SyncMap[K comparable, V any] struct {
	// contains filtered or unexported fields
}

SyncMap is a thread-safe implementation of Map using sync.Map. Note: the internal implementation of sync.Map requires a comparable type to run the CompareAndSwap operation. To circumvent this, attach an equal function to the map upon creation.

func NewSyncMap

func NewSyncMap[K comparable, V any](equalFn func(V, V) bool) *SyncMap[K, V]

NewSyncMap creates a new instance of SyncMap. The equalFn parameter is required to decide how two values of type V are compared, but can be nil if V is comparable.

func SyncMapFromMap added in v0.4.0

func SyncMapFromMap[K comparable, V any](m map[K]V, equalFn func(V, V) bool) *SyncMap[K, V]

SyncMapFromMap creates a new instance of SyncMap from values in the provided map.

func (*SyncMap[K, V]) All added in v0.6.0

func (s *SyncMap[K, V]) All() iter.Seq2[K, V]

All returns an iterator over key-value pairs in the map. The iteration order is not guaranteed to be consistent.

func (*SyncMap[K, V]) Clear

func (s *SyncMap[K, V]) Clear()

Clear removes all items from the store.

func (*SyncMap[K, V]) CompareAndSwap

func (s *SyncMap[K, V]) CompareAndSwap(key K, oldValue, newValue V) bool

CompareAndSwap executes the compare-and-swap operation for a key.

func (*SyncMap[K, V]) Delete

func (s *SyncMap[K, V]) Delete(key K)

Delete removes the key from the store.

func (*SyncMap[K, V]) Equals added in v0.3.2

func (s *SyncMap[K, V]) Equals(other Map[K, V], equalFn func(a, b V) bool) bool

Equals reports whether the logical content of this map and the other map is the same. Requires equalFn to be provided to decide how two values of type V are compared.

func (*SyncMap[K, V]) Get

func (s *SyncMap[K, V]) Get(key K) (V, bool)

Get retrieves the value for the given key.

func (*SyncMap[K, V]) GetAll

func (s *SyncMap[K, V]) GetAll() map[K]V

GetAll returns all key-value pairs in the store.

func (*SyncMap[K, V]) GetMany

func (s *SyncMap[K, V]) GetMany(keys []K) map[K]V

GetMany retrieves multiple keys at once.

func (*SyncMap[K, V]) Keys added in v0.6.0

func (s *SyncMap[K, V]) Keys() iter.Seq[K]

Keys returns an iterator over keys in the map. The iteration order is not guaranteed to be consistent.

func (*SyncMap[K, V]) Len

func (s *SyncMap[K, V]) Len() int

Len returns the number of items in the store. Note: This is an O(n) operation as sync.Map doesn't track its size.

func (*SyncMap[K, V]) LoadAndDelete added in v0.5.0

func (s *SyncMap[K, V]) LoadAndDelete(key K) (V, bool)

LoadAndDelete deletes the value for a key, returning the previous value if any.

func (*SyncMap[K, V]) LoadOrStore added in v0.5.0

func (s *SyncMap[K, V]) LoadOrStore(key K, value V) (V, bool)

LoadOrStore returns the existing value for the key if present. Otherwise, it stores and returns the given value. The loaded result is true if the value was loaded, false if stored.

func (*SyncMap[K, V]) Range

func (s *SyncMap[K, V]) Range(f func(key K, value V) bool)

Range calls f sequentially for each key and value present in the map. If f returns false, range stops the iteration.

func (*SyncMap[K, V]) Set

func (s *SyncMap[K, V]) Set(key K, value V)

Set stores a value for the given key.

func (*SyncMap[K, V]) SetMany

func (s *SyncMap[K, V]) SetMany(entries map[K]V)

SetMany sets multiple key-value pairs at once.

func (*SyncMap[K, V]) Swap

func (s *SyncMap[K, V]) Swap(key K, value V) (V, bool)

Swap swaps the value for a key and returns the previous value if any.

func (*SyncMap[K, V]) Values added in v0.6.0

func (s *SyncMap[K, V]) Values() iter.Seq[V]

Values returns an iterator over values in the map. The iteration order is not guaranteed to be consistent.

type SyncMapSet added in v0.4.0

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

SyncMapSet is a thread-safe Set implementation backed by sync.Map. Internally it stores the items as keys in the sync.Map with an empty struct{} value. All operations are safe for concurrent use by multiple goroutines.

NOTE: Operations like Len, Slice and Clear iterate over the entire map. They run in O(n) time and may allocate, just like their RWMutex counterpart. If you need high-frequency Len or Slice under heavy write load, prefer the RWMutex variant which can maintain a separate atomic counter.

func NewSyncMapSet added in v0.4.0

func NewSyncMapSet[T comparable]() *SyncMapSet[T]

NewSyncMapSet creates a new instance of SyncMapSet.

func (*SyncMapSet[T]) Add added in v0.4.0

func (s *SyncMapSet[T]) Add(item T) (added bool)

Add stores an item in the set.

func (*SyncMapSet[T]) All added in v0.6.0

func (s *SyncMapSet[T]) All() iter.Seq[T]

All returns an iterator over all items in the set. The iteration order is not guaranteed to be consistent.

func (*SyncMapSet[T]) Clear added in v0.4.0

func (s *SyncMapSet[T]) Clear()

Clear removes all items from the set.

func (*SyncMapSet[T]) Delete added in v0.5.0

func (s *SyncMapSet[T]) Delete(item T) (removed bool)

Delete removes an item from the set.

func (*SyncMapSet[T]) Has added in v0.4.0

func (s *SyncMapSet[T]) Has(item T) bool

Has returns true if the item is in the set, otherwise false.

func (*SyncMapSet[T]) Len added in v0.4.0

func (s *SyncMapSet[T]) Len() int

Len returns the number of items in the set.

func (*SyncMapSet[T]) Range added in v0.4.0

func (s *SyncMapSet[T]) Range(f func(item T) bool)

Range calls f sequentially for each item present in the set. If f returns false, range stops the iteration.

func (*SyncMapSet[T]) Slice added in v0.4.0

func (s *SyncMapSet[T]) Slice() []T

Slice returns a copy of the set as a slice.

Jump to

Keyboard shortcuts

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