utils

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2025 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrActiveStream = errors.New("a stream is already active. Only one active stream is allowed at a time")

ErrActiveStream represents the error when attempting to create a new stream while one is already active. The system only allows a single active stream at any given time.

Functions

func CountAppearances added in v0.1.5

func CountAppearances[T comparable](items []T) map[T]int

CountAppearances returns the number of appearances each item have.

func DecodeOrderPreservingVarUint64

func DecodeOrderPreservingVarUint64(bytes []byte) (uint64, int, error)

DecodeOrderPreservingVarUint64 decodes the number from the bytes obtained from method 'EncodeOrderPreservingVarUint64'. It returns the decoded number, the number of bytes that are consumed in the process, and an error if the input bytes are invalid.

func EncodeOrderPreservingVarUint64

func EncodeOrderPreservingVarUint64(number uint64) []byte

EncodeOrderPreservingVarUint64 returns a byte-representation for a uint64 number such that all zero-bits starting bytes are trimmed in order to reduce the length of the array For preserving the order in a default bytes-comparison, first byte contains the number of remaining bytes. The presence of first byte also allows to use the returned bytes as part of other larger byte array such as a composite-key representation in db.

func FileExists

func FileExists(path string) bool

FileExists returns true if a file path exists.

func Must

func Must(err error, msg ...string)

Must panics given an error.

func MustRead added in v0.1.6

func MustRead(source io.Reader, size int) []byte

MustRead reads a byte array of the given size from the source. It panics if the read fails, or cannot read the requested size. "crypto/rand" and "math/rand" never fail and always returns the correct length.

func ProcessErr

func ProcessErr(err error, msg string) error

ProcessErr wraps a non-nil error with a message using %w for unwrapping. Returns nil if the error is nil, otherwise returns the wrapped error. Example to the call of the function: utils.ProcessErr(g.Wait(), "sidecar has been stopped").

Types

type LazyJSON

type LazyJSON struct {
	O      any
	Indent string
}

LazyJSON will lazily marshal a struct for logging purposes.

func (*LazyJSON) String

func (lj *LazyJSON) String() string

String marshals the give object as JSON.

type Slots

type Slots struct {
	// contains filtered or unexported fields
}

Slots manages a resource counter, allowing callers to block until at least one slot is available.

func NewSlots

func NewSlots(size int64) *Slots

NewSlots creates a new Slots instance with the specified initial size.

func (*Slots) Acquire

func (w *Slots) Acquire(ctx context.Context, n int64)

Acquire blocks until at least one slot is available or the context is canceled. It then reduces the available slot count by n.

func (*Slots) Broadcast

func (w *Slots) Broadcast()

Broadcast wakes up all goroutines waiting to acquire slots.

func (*Slots) Load

func (w *Slots) Load(_ *testing.T) int64

Load returns the current number of available slots.

func (*Slots) Release

func (w *Slots) Release(n int64)

Release adds 'n' slots back to the pool and signals a waiting goroutine.

func (*Slots) Store

func (w *Slots) Store(_ *testing.T, s int64)

Store sets the current number of available slots.

type SyncMap

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

SyncMap is a wrapper for sync.Map. It enhances readability by explicitly specifying the key and value types at compile time. This also minimizes boilerplate code needed for type casting.

func (*SyncMap[K, V]) Clear

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

Clear mirrors sync.Map's method. See sync.Map for detailed documentation.

func (*SyncMap[K, V]) CompareAndDelete

func (m *SyncMap[K, V]) CompareAndDelete(key K, value V) (deleted bool)

CompareAndDelete mirrors sync.Map's method. See sync.Map for detailed documentation.

func (*SyncMap[K, V]) CompareAndSwap

func (m *SyncMap[K, V]) CompareAndSwap(key K, oldVal, newVal V) (swapped bool)

CompareAndSwap mirrors sync.Map's method. See sync.Map for detailed documentation.

func (*SyncMap[K, V]) Count

func (m *SyncMap[K, V]) Count() int

Count counts the number of items in the map. It iterates over the map, so it is best to avoid it in production.

func (*SyncMap[K, V]) Delete

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

Delete mirrors sync.Map's method. See sync.Map for detailed documentation.

func (*SyncMap[K, V]) IterItems

func (m *SyncMap[K, V]) IterItems() iter.Seq2[K, V]

IterItems allows iterating over the items' key value pair. Example:

for k, v := range m.IterItems() {
    fmt.Printf("%v: %v\n", k, v)
}

func (*SyncMap[K, V]) IterKeys

func (m *SyncMap[K, V]) IterKeys() iter.Seq[K]

IterKeys allows iterating over the items' keys. Example:

for k := range m.IterKeys() {
    fmt.Printf("%v\n", k)
}

func (*SyncMap[K, V]) IterValues

func (m *SyncMap[K, V]) IterValues() iter.Seq[V]

IterValues allows iterating over the items' values. Example:

for v := range m.IterValues() {
    fmt.Printf("%v\n", v)
}

func (*SyncMap[K, V]) Load

func (m *SyncMap[K, V]) Load(key K) (value V, ok bool)

Load mirrors sync.Map's method. See sync.Map for detailed documentation.

func (*SyncMap[K, V]) LoadAndDelete

func (m *SyncMap[K, V]) LoadAndDelete(key K) (value V, loaded bool)

LoadAndDelete mirrors sync.Map's method. See sync.Map for detailed documentation.

func (*SyncMap[K, V]) LoadOrStore

func (m *SyncMap[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool)

LoadOrStore mirrors sync.Map's method. See sync.Map for detailed documentation.

func (*SyncMap[K, V]) Range

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

Range mirrors sync.Map's method. See sync.Map for detailed documentation.

func (*SyncMap[K, V]) Store

func (m *SyncMap[K, V]) Store(key K, value V)

Store mirrors sync.Map's method. See sync.Map for detailed documentation.

func (*SyncMap[K, V]) Swap

func (m *SyncMap[K, V]) Swap(key K, value V) (previous V, loaded bool)

Swap mirrors sync.Map's method. See sync.Map for detailed documentation.

Jump to

Keyboard shortcuts

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