util

package
v0.13.3 Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2025 License: GPL-3.0 Imports: 49 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrIntRangeSetEmpty            = errors.New("set is empty")
	ErrIntRangeSetOutOfRange       = errors.New("value out of range")
	ErrIntRangeReturnedValueInSet  = errors.New("value returned is already in the set")
	ErrIntRangeSetValueUnavailable = errors.New("value not currently present in the set")
)
View Source
var RXTotal, TXTotal int64

Functions

func AllPermutations

func AllPermutations[S ~[]E, E any](s S) iter.Seq[iter.Seq2[int, E]]

AllPermutations returns an iterator over all permutations of the given slice. Each permutation can then be iterated over.

func Atof

func Atof(s string) (float64, error)

atof is a utility for parsing floating point values that sends errors to the logging system.

func CacheCullObjects added in v0.13.0

func CacheCullObjects(maxBytes int64) error

func CacheRetrieveObject added in v0.13.0

func CacheRetrieveObject(path string, obj any) (time.Time, error)

func CacheStoreObject added in v0.13.0

func CacheStoreObject(path string, obj any) error

func CheckJSON

func CheckJSON[T any](contents []byte, e *ErrorLogger)

CheckJSON checks whether the provided JSON is syntactically valid and then typechecks it with respect to the provided type T.

func CommaKeyExpand

func CommaKeyExpand[S ~string, T any](in map[S]T) (map[S]T, error)

Given a map from strings to some type T where the keys are assumed to be of the form "foo,bar,bat", return a new map where each comma-delineated string in the keys has its own entry in the returned map. Returns an error if a key is repeated.

func CutAtSpace added in v0.13.3

func CutAtSpace(s string) (string, string)

CutAtSpace is like strings.Cut(s, " ") but preserves the space in the second return value.

func CutFunc added in v0.13.3

func CutFunc(s string, f func(rune) bool) (string, string, bool)

Similar to strings.Cut, but cuts at the first rune where `f` return true; note that the second returned string includes the cutpoint rune.

func DebuggerIsRunning

func DebuggerIsRunning() bool

DebuggerIsRunning returns true if we are running under a debugger; this allows inhibiting various timeouts that may otherwise get in the way of debugging. Currently only detects dlv (TODO others as applicable).

func DeleteSliceElement

func DeleteSliceElement[V any](s []V, i int) []V

DeleteSliceElement deletes the i-th element of the given slice, returning the resulting slice.

Note that the provided slice s is modified!

func DeltaDecode

func DeltaDecode[T constraints.Integer](d []T) []T

func DeltaDecodeBytes

func DeltaDecodeBytes(ref, delta []byte) []byte

func DeltaDecodeBytesSlice

func DeltaDecodeBytesSlice(encoded [][]byte) [][]byte

func DeltaEncode

func DeltaEncode[T constraints.Integer](d []T) []T

func DeltaEncodeBytes

func DeltaEncodeBytes(ref, next []byte) []byte

func DeltaEncodeBytesSlice

func DeltaEncodeBytesSlice(data [][]byte) [][]byte

func DumpHeldMutexes

func DumpHeldMutexes(lg *log.Logger) string

func DuplicateSlice

func DuplicateSlice[V any](s []V) []V

DuplicateSlice returns a newly-allocated slice that is a copy of the provided one.

func FilterSeq

func FilterSeq[T any](seq iter.Seq[T], pred func(T) bool) iter.Seq[T]

FilterSeq applies uses the given predicate function to filter the elements given by a sequence iterator, returning an iterator over the filtered elements.

func FilterSeq2

func FilterSeq2[K, V any](seq iter.Seq2[K, V], pred func(K, V) bool) iter.Seq2[K, V]

func FilterSlice

func FilterSlice[V any](s []V, pred func(V) bool) []V

FilterSlice applies the given filter function pred to the given slice, returning a new slice that only contains elements where pred returned true.

func FilterSliceInPlace

func FilterSliceInPlace[V any](s []V, pred func(V) bool) []V

FilterSliceInPlace applies the given filter function pred to the given slice, returning a slice constructed from the provided slice's memory that only contains elements where pred returned true.

func FindTimeAtOrBefore added in v0.13.0

func FindTimeAtOrBefore(times []time.Time, t time.Time) (int, error)

FindTimeAtOrBefore finds the index of the time at or before t in a sorted slice of times. Returns the index and an error if times is empty or t is out of range.

func FirstSortedMapEntry added in v0.13.0

func FirstSortedMapEntry[K constraints.Ordered, V any](m map[K]V) (K, V)

func GetLoggedRPCBandwidth

func GetLoggedRPCBandwidth() (int64, int64)

func GetResourcesFS

func GetResourcesFS() fs.StatFS

func Hash

func Hash(r io.Reader) ([]byte, error)

func HashString64

func HashString64(s string) uint64

func InitFlightRecorder added in v0.13.2

func InitFlightRecorder(lg *log.Logger)

InitFlightRecorder initializes the flight recorder to continuously record the last 10 seconds of execution traces.

func InsertSliceElement

func InsertSliceElement[V any](s []V, i int, v V) []V

InsertSliceElement inserts the given value v at the index i in the slice s, moving all elements after i one place forward.

func IsAllLetters

func IsAllLetters(s string) bool

func IsAllNumbers

func IsAllNumbers(s string) bool

func IsRPCServerError

func IsRPCServerError(err error) bool

func LoadResourceBytes

func LoadResourceBytes(path string) []byte

func MakeMessagepackClientCodec

func MakeMessagepackClientCodec(conn io.ReadWriteCloser) rpc.ClientCodec

func MakeMessagepackServerCodec

func MakeMessagepackServerCodec(conn io.ReadWriteCloser, lg *log.Logger) rpc.ServerCodec

func MapContains

func MapContains[K comparable, V any](m map[K]V, pred func(K, V) bool) bool

func MapLookupFunc

func MapLookupFunc[K comparable, V any](m map[K]V, pred func(K, V) bool) (K, V, bool)

func MapSeq

func MapSeq[T, U any](seq iter.Seq[T], f func(T) U) iter.Seq[U]

func MapSeq2

func MapSeq2[K, V, K2, V2 any](seq iter.Seq2[K, V], f func(K, V) (K2, V2)) iter.Seq2[K2, V2]

func MapSlice

func MapSlice[F, T any](from []F, xform func(F) T) []T

MapSlice returns the slice that is the result of applying the provided xform function to all the elements of the given slice.

func MonitorCPUUsage

func MonitorCPUUsage(limit int, panicIfWedged bool, lg *log.Logger)

func MonitorMemoryUsage

func MonitorMemoryUsage(triggerMB int, incMB int, lg *log.Logger)

MonitorMemoryUsage launches a goroutine that periodically checks how much memory is in use; if it's above the threshold, it writes out a memory profile file and then bumps the threshold by the given increment.

func ReduceMap

func ReduceMap[K comparable, V any, R any](m map[K]V, reduce func(K, V, R) R, initial R) R

ReduceMap applies the provided reduction function to the given map, starting with the provided initial value. The update rule applied is result=reduce(key, value, result), where the initial value of result is given by the initial parameter.

func ReduceSlice

func ReduceSlice[V any, R any](s []V, reduce func(V, R) R, initial R) R

ReduceSlice applies the provided reduction function to the given slice, starting with the provided initial value. The update rule applied is result=reduce( value, result), where the initial value of result is given by the initial parameter.

func Select

func Select[T any](sel bool, a, b T) T

func SelectInTwoEdits

func SelectInTwoEdits(str string, seq iter.Seq[string], dist1, dist2 []string) ([]string, []string)

Given a string iterator and a base string, return two arrays of strings from the iterator that are respectively within one or two edits of the base string. // https://en.wikipedia.org/wiki/Levenshtein_distance

func Seq2Concat added in v0.13.0

func Seq2Concat[K, V any](seq ...iter.Seq2[K, V]) iter.Seq2[K, V]

func Seq2Keys

func Seq2Keys[K, V any](seq iter.Seq2[K, V]) iter.Seq[K]

func Seq2Single added in v0.13.0

func Seq2Single[K, V any](k K, v V) iter.Seq2[K, V]

func Seq2Values

func Seq2Values[K, V any](seq iter.Seq2[K, V]) iter.Seq[V]

func SeqConcat added in v0.13.0

func SeqConcat[V any](seq ...iter.Seq[V]) iter.Seq[V]

func SeqContains

func SeqContains[T comparable](seq iter.Seq[T], v T) bool

func SeqContainsFunc

func SeqContainsFunc[T any](seq iter.Seq[T], check func(T) bool) bool

func SeqLookupFunc

func SeqLookupFunc[T any](seq iter.Seq[T], check func(T) bool) (T, bool)

func SeqMaxIndexFunc

func SeqMaxIndexFunc[K, V any, W constraints.Ordered](seq iter.Seq2[K, V], weight func(K, V) W) (K, bool)

func SeqMinIndexFunc

func SeqMinIndexFunc[K, V any, W constraints.Ordered](seq iter.Seq2[K, V], weight func(K, V) W) (K, bool)

func SeqSingle added in v0.13.0

func SeqSingle[V any](v V) iter.Seq[V]

func SizeOf

func SizeOf(obj any, w io.Writer, printMembers bool, threshold int64) int64

SizeOf returns the total size in bytes of the given object. If printMembers is true and obj is a struct, it prints the size of each field. If threshold > 0, prints any element that is threshold bytes or larger.

func SortedMap added in v0.13.0

func SortedMap[K constraints.Ordered, V any](m map[K]V) iter.Seq2[K, V]

func SortedMapKeys

func SortedMapKeys[K constraints.Ordered, V any](m map[K]V) []K

SortedMapKeys returns the keys of the given map, sorted from low to high.

func StopShouting

func StopShouting(orig string) string

StopShouting turns text of the form "UNITED AIRLINES" to "United Airlines"

func TransposeStrings added in v0.13.0

func TransposeStrings(strs []string) ([]string, error)

func TypeCheckJSON

func TypeCheckJSON[T any](json interface{}) bool

TypeCheckJSON returns a Boolean indicating whether the provided raw unmarshaled JSON values are type-compatible with the given type T.

func UnmarshalJSON

func UnmarshalJSON[T any](r io.Reader, out *T) error

func UnmarshalJSONBytes

func UnmarshalJSONBytes[T any](b []byte, out *T) error

Unmarshal the bytes into the given type but go through some efforts to return useful error messages when the JSON is invalid...

func WalkResources

func WalkResources(root string, fn func(path string, d fs.DirEntry, filesystem fs.FS, err error) error) error

func WrapText

func WrapText(s string, columnLimit int, indent int, wrapAll bool, noSpace bool) (string, int)

Types

type AtomicBool

type AtomicBool struct {
	atomic.Bool
}

AtomicBool is a simple wrapper around atomic.Bool that adds support for JSON marshaling/unmarshaling.

func (AtomicBool) MarshalJSON

func (a AtomicBool) MarshalJSON() ([]byte, error)

func (*AtomicBool) UnmarshalJSON

func (a *AtomicBool) UnmarshalJSON(data []byte) error

type ByteCount

type ByteCount int64

func (ByteCount) String

func (b ByteCount) String() string

type CompressedConn

type CompressedConn struct {
	net.Conn
	// contains filtered or unexported fields
}

func MakeCompressedConn

func MakeCompressedConn(c net.Conn) (*CompressedConn, error)

func (*CompressedConn) Close

func (c *CompressedConn) Close() error

func (*CompressedConn) Read

func (c *CompressedConn) Read(b []byte) (n int, err error)

func (*CompressedConn) Write

func (c *CompressedConn) Write(b []byte) (n int, err error)

type DuplicateJSONKey added in v0.13.3

type DuplicateJSONKey struct {
	Path string // JSON path to the duplicate (e.g., "scenarios.P50.inbound_rates")
	Key  string // The duplicate key name
}

DuplicateJSONKey represents a duplicate key found in JSON.

func FindDuplicateJSONKeys added in v0.13.3

func FindDuplicateJSONKeys(data []byte) []DuplicateJSONKey

FindDuplicateJSONKeys scans JSON content and returns all duplicate keys found. It uses the json.Decoder token-based API to walk the JSON structure while tracking seen keys at each object nesting level.

type ErrorLogger

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

ErrorLogger is a small utility class used to log errors when validating the parsed JSON scenarios. It tracks context about what is currently being validated and accumulates multiple errors, making it possible to log errors while still continuing validation.

func (*ErrorLogger) CheckDepth

func (e *ErrorLogger) CheckDepth(d int)

func (*ErrorLogger) CurrentDepth

func (e *ErrorLogger) CurrentDepth() int

func (*ErrorLogger) Error

func (e *ErrorLogger) Error(err error)

func (*ErrorLogger) ErrorString

func (e *ErrorLogger) ErrorString(s string, args ...interface{})

func (*ErrorLogger) HaveErrors

func (e *ErrorLogger) HaveErrors() bool

func (*ErrorLogger) Pop

func (e *ErrorLogger) Pop()

func (*ErrorLogger) PrintErrors

func (e *ErrorLogger) PrintErrors(lg *log.Logger)

func (*ErrorLogger) Push

func (e *ErrorLogger) Push(s string)

func (*ErrorLogger) String

func (e *ErrorLogger) String() string

type GCSClient added in v0.13.0

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

func MakeGCSClient added in v0.13.0

func MakeGCSClient(bucket string, config GCSClientConfig) (*GCSClient, error)

MakeGCSClient creates a GCS client with the given bucket and configuration. If Credentials is nil, creates an unauthenticated client. If Context is nil, uses context.Background(). If Timeout is zero, uses 30 seconds default.

func (*GCSClient) GetReader added in v0.13.0

func (g *GCSClient) GetReader(objectName string) (io.ReadCloser, error)

GetReader returns a ReadCloser for downloading an object from the Google Cloud Storage bucket. The caller is responsible for closing the returned ReadCloser.

func (*GCSClient) GetURL added in v0.13.0

func (g *GCSClient) GetURL(objectName string, lifetime time.Duration) (string, error)

GetURL returns a signed URL for downloading an object from the bucket. The lifetime parameter specifies how long the URL should remain valid. For unauthenticated clients, it returns a public URL (lifetime is ignored). For authenticated clients, it returns a V4 signed URL that expires after the specified duration.

func (*GCSClient) List added in v0.13.0

func (g *GCSClient) List(prefix string) (map[string]int64, error)

List returns a map of object names to their sizes from the Google Cloud Storage bucket. It uses the GCS JSON API v1. If prefix is non-empty, only objects with that prefix are returned.

type GCSClientConfig added in v0.13.0

type GCSClientConfig struct {
	Context     context.Context // Optional: defaults to context.Background()
	Credentials []byte          // Optional: service account JSON; if nil, creates unauthenticated client
	Timeout     time.Duration   // Optional: HTTP client timeout; defaults to 30 seconds
}

GCSClientConfig holds configuration options for creating a GCS client

type GCSListResponse

type GCSListResponse struct {
	Items         []GCSObject `json:"items"`
	NextPageToken string      `json:"nextPageToken"`
}

GCSListResponse represents the JSON response from the GCS objects list API

type GCSObject

type GCSObject struct {
	Name string `json:"name"`
	Size string `json:"size"`
}

GCSObject represents the minimal object metadata we need from the GCS API response

type IntRangeSet

type IntRangeSet struct {
	First, Last   int
	AvailableBits []uint64
}

func MakeIntRangeSet

func MakeIntRangeSet(first, last int) *IntRangeSet

[first,last]

func (*IntRangeSet) Clone

func (s *IntRangeSet) Clone() *IntRangeSet

func (*IntRangeSet) Count

func (s *IntRangeSet) Count() int

func (*IntRangeSet) GetRandom

func (s *IntRangeSet) GetRandom(r *rand.Rand) (int, error)

func (*IntRangeSet) InRange

func (s *IntRangeSet) InRange(v int) bool

func (*IntRangeSet) IsAvailable

func (s *IntRangeSet) IsAvailable(v int) bool

func (*IntRangeSet) Return

func (s *IntRangeSet) Return(v int) error

func (*IntRangeSet) Take

func (s *IntRangeSet) Take(v int) error

type JSONChecker

type JSONChecker interface {
	CheckJSON(json interface{}) bool
}

JSONChecker is an interface that allows types that implement custom JSON unmarshalers to check whether raw unmarshled JSON types are compatible with their underlying type.

type LoggingClientCodec

type LoggingClientCodec struct {
	rpc.ClientCodec
	// contains filtered or unexported fields
}

func MakeLoggingClientCodec

func MakeLoggingClientCodec(label string, c rpc.ClientCodec, lg *log.Logger) *LoggingClientCodec

func (*LoggingClientCodec) ReadResponseHeader

func (c *LoggingClientCodec) ReadResponseHeader(r *rpc.Response) error

func (*LoggingClientCodec) WriteRequest

func (c *LoggingClientCodec) WriteRequest(r *rpc.Request, v any) error

type LoggingConn

type LoggingConn struct {
	net.Conn
	// contains filtered or unexported fields
}

func MakeLoggingConn

func MakeLoggingConn(c net.Conn, lg *log.Logger) *LoggingConn

func (*LoggingConn) Read

func (c *LoggingConn) Read(b []byte) (n int, err error)

func (*LoggingConn) Write

func (c *LoggingConn) Write(b []byte) (n int, err error)

type LoggingMutex

type LoggingMutex struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func (*LoggingMutex) Lock

func (l *LoggingMutex) Lock(lg *log.Logger)

func (*LoggingMutex) LogValue

func (l *LoggingMutex) LogValue() slog.Value

func (*LoggingMutex) String

func (l *LoggingMutex) String(lg *log.Logger) string

func (*LoggingMutex) Unlock

func (l *LoggingMutex) Unlock(lg *log.Logger)

type LoggingServerCodec

type LoggingServerCodec struct {
	rpc.ServerCodec
	// contains filtered or unexported fields
}

func MakeLoggingServerCodec

func MakeLoggingServerCodec(label string, c rpc.ServerCodec, lg *log.Logger) *LoggingServerCodec

func (*LoggingServerCodec) Close

func (c *LoggingServerCodec) Close() error

func (*LoggingServerCodec) ReadRequestBody

func (c *LoggingServerCodec) ReadRequestBody(body any) error

func (*LoggingServerCodec) ReadRequestHeader

func (c *LoggingServerCodec) ReadRequestHeader(r *rpc.Request) error

func (*LoggingServerCodec) WriteResponse

func (c *LoggingServerCodec) WriteResponse(r *rpc.Response, body any) error

type ObjectArena

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

func (*ObjectArena[T]) AllocClear

func (a *ObjectArena[T]) AllocClear() *T

func (*ObjectArena[T]) Cap

func (a *ObjectArena[T]) Cap() int

func (*ObjectArena[T]) Reset

func (a *ObjectArena[T]) Reset()

type OneOf

type OneOf[A, B any] struct {
	A *A
	B *B
}

func (OneOf[A, B]) CheckJSON

func (o OneOf[A, B]) CheckJSON(json interface{}) bool

func (OneOf[A, B]) MarshalJSON

func (o OneOf[A, B]) MarshalJSON() ([]byte, error)

func (*OneOf[A, B]) UnmarshalJSON

func (o *OneOf[A, B]) UnmarshalJSON(j []byte) error

type Optional

type Optional[T any] struct {
	Value T
	IsSet bool
}

func (Optional[T]) Get

func (o Optional[T]) Get() T

func (Optional[T]) GetOr

func (o Optional[T]) GetOr(v T) T

func (*Optional[T]) Set

func (o *Optional[T]) Set(v T)

type OrderedMap

type OrderedMap struct {
	orderedmap.OrderedMap
}

func (*OrderedMap) CheckJSON

func (o *OrderedMap) CheckJSON(json interface{}) bool

type Profiler

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

func CreateProfiler

func CreateProfiler(cpu, mem string) (Profiler, error)

func (*Profiler) Cleanup

func (p *Profiler) Cleanup()

type ResourceReadCloser

type ResourceReadCloser interface {
	io.Reader
	Close()
}

Unfortunately, unlike io.ReadCloser, the zstd Decoder's Close() method doesn't return an error, so we need to make our own custom ReadCloser interface.

func LoadResource

func LoadResource(path string) ResourceReadCloser

LoadResource provides a ResourceReadCloser to access the specified file from the resources directory; if it's zstd compressed, the Reader will handle decompression transparently. It panics if the file is not found since missing resources are pretty much impossible to recover from.

type RootFS

type RootFS struct{}

func (RootFS) Open

func (r RootFS) Open(filename string) (fs.File, error)

type SingleOrArray

type SingleOrArray[V any] []V

SingleOrArray makes it possible to have an object in a JSON file that may be initialized with either a single value or an array of values. In either case, the object's value is represented by a slice of the underlying type.

func (*SingleOrArray[V]) CheckJSON

func (s *SingleOrArray[V]) CheckJSON(json interface{}) bool

func (*SingleOrArray[V]) UnmarshalJSON

func (s *SingleOrArray[V]) UnmarshalJSON(b []byte) error

type TempFileRegistry

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

func MakeTempFileRegistry

func MakeTempFileRegistry(lg *log.Logger) *TempFileRegistry

func (*TempFileRegistry) RegisterPath

func (t *TempFileRegistry) RegisterPath(path string)

func (*TempFileRegistry) RemoveAll

func (t *TempFileRegistry) RemoveAll()

func (*TempFileRegistry) RemoveAllPrefix

func (t *TempFileRegistry) RemoveAllPrefix(prefix string)

type TextWrapConfig added in v0.13.0

type TextWrapConfig struct {
	ColumnLimit int
	Indent      int
	WrapAll     bool
	WrapNoSpace bool
}

func (TextWrapConfig) Wrap added in v0.13.0

func (cfg TextWrapConfig) Wrap(s string) (string, int)

type TimeInterval added in v0.13.0

type TimeInterval [2]time.Time

TimeInterval represents a time interval with start and end times

func FindTimeIntervals added in v0.13.0

func FindTimeIntervals(times []time.Time, d time.Duration) []TimeInterval

FindTimeIntervals creates TimeIntervals from a series of sorted times. Given a series of sorted times and a maximum duration, it returns intervals where if the duration between two successive times is greater than d, then the current interval ends at the first time and a new interval starts at the second time.

func IntersectAllIntervals added in v0.13.2

func IntersectAllIntervals(intervals ...[]TimeInterval) []TimeInterval

IntersectAllIntervals intersects multiple sets of TimeIntervals and returns the common intervals where all input interval sets overlap.

func IntersectIntervals added in v0.13.0

func IntersectIntervals(a, b []TimeInterval) []TimeInterval

IntersectIntervals returns the intersection of two sets of TimeIntervals

func (TimeInterval) Contains added in v0.13.0

func (ti TimeInterval) Contains(t time.Time) bool

Contains checks if the interval contains the given time

func (TimeInterval) Duration added in v0.13.0

func (ti TimeInterval) Duration() time.Duration

Duration returns the duration of the interval

func (TimeInterval) End added in v0.13.0

func (ti TimeInterval) End() time.Time

End returns the end time of the interval

func (TimeInterval) Start added in v0.13.0

func (ti TimeInterval) Start() time.Time

Start returns the start time of the interval

type TransientMap

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

TransientMap represents a set of objects with a built-in expiry time in the future; after an item's time passes, it is automatically removed from the set.

func NewTransientMap

func NewTransientMap[K comparable, V any]() *TransientMap[K, V]

func (*TransientMap[K, V]) Add

func (t *TransientMap[K, V]) Add(key K, value V, d time.Duration)

Add adds a given value to the set; it will no longer be there after the specified duration has passed.

func (*TransientMap[K, V]) Delete

func (t *TransientMap[K, V]) Delete(key K)

Delete deletes the item in the map with the given key, if present.

func (*TransientMap[K, V]) Get

func (t *TransientMap[K, V]) Get(key K) (V, bool)

Get looks up the given key in the map and returns its value and a Boolean that indicates whether it was found.

Jump to

Keyboard shortcuts

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