safemap

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package safemap provides a generic, concurrency-safe map wrapper.

All operations are protected by a lockmetrics.RWMutex for concurrent access with full Prometheus instrumentation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ClonePtrMap

func ClonePtrMap[K comparable, V any](m map[K]*V) map[K]*V

ClonePtrMap returns a deep copy of a map whose values are pointers: the returned map has the same keys, but each value points to a freshly allocated copy of the pointed-to value (nil pointers are preserved as nil).

This differs from the standard library's maps.Clone, which copies the map structure but shares the underlying pointers — insufficient for a stable point-in-time snapshot, where the copy must not observe later mutations of the original values. Returns nil when m is nil.

func CloneRegionalPtrMap

func CloneRegionalPtrMap[R comparable, K comparable, V any](m map[R]map[K]*V) map[R]map[K]*V

CloneRegionalPtrMap returns a deep copy of the region-nested pointer maps used pervasively by service backends (outer key = region, inner key = resource name, value = *T). Both map levels are reallocated and each value is value-copied via ClonePtrMap. Returns nil when m is nil.

Types

type Map

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

Map is a generic concurrency-safe map.

The zero value is not usable; always create via New.

func New

func New[K comparable, V any](name string) *Map[K, V]

New creates a new Map. The name appears as the "lock" label in Prometheus metrics and should be a stable, human-readable identifier (e.g. "sqs.queue.my-queue.tags").

func (*Map[K, V]) Clear

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

Clear removes all entries from the map.

func (*Map[K, V]) Clone

func (m *Map[K, V]) Clone() map[K]V

Clone returns a shallow copy of the underlying map.

func (*Map[K, V]) Close

func (m *Map[K, V]) Close()

Close removes the Map's underlying lockmetrics.RWMutex from the global metrics registry. It should be called when the Map is no longer needed to prevent unbounded growth of the Prometheus collector. After Close is called, the Map must not be used.

func (*Map[K, V]) Delete

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

Delete removes key from the map.

func (*Map[K, V]) Get

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

Get returns the value associated with key and whether the key was found.

func (*Map[K, V]) Keys

func (m *Map[K, V]) Keys() []K

Keys returns all keys as a slice in unspecified order.

func (*Map[K, V]) Len

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

Len returns the number of entries.

func (*Map[K, V]) Range

func (m *Map[K, V]) Range(f func(K, V) bool)

Range calls f sequentially for each key-value pair in unspecified order. The map is read-locked for the entire iteration; f must not call methods on the same Map to avoid deadlocks. Iteration stops early if f returns false.

func (*Map[K, V]) Set

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

Set adds or updates key to value.

func (*Map[K, V]) Values

func (m *Map[K, V]) Values() []V

Values returns all values as a slice in unspecified order.

Jump to

Keyboard shortcuts

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