go.temporal.io/server/common/util/util.go
197 LOC · 88 covered · 109 uncovered · 42 ranges · 15449 concepts · 35 introducers · 7653 tests
File neighbourhood
The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.
Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file
In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.
Graph controls are ready.
Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.
// util contains small standalone utility functions. This should have no
// dependencies on other server packages.
package util
import (
"cmp"
"context"
"maps"
"slices"
"time"
)
// MinTime returns the earlier of two given time.Time
if a.Before(b) {
}
}
// MaxTime returns the latest of the given time.Time values.
latest := first
for _, t := range rest {
if t.After(latest) {
}
}
}
// NextAlignedTime returns the earliest time after `t` that is aligned to an integer multiple
// of `align` since the unix epoch.
func NextAlignedTime(t time.Time, align time.Duration) time.Time {
return time.Unix(0, (t.UnixNano()/int64(align)+1)*int64(align))
}
// SortSlice sorts the given slice of an ordered type.
// Sort is not guaranteed to be stable.
func SortSlice[S ~[]E, E cmp.Ordered](slice S) {
slices.Sort(slice)
}
// SliceHead returns the first n elements of s. n may be greater than len(s).
if n < len(s) {
return s[:n]
}
}
// SliceTail returns the last n elements of s. n may be greater than len(s).
if extra := len(s) - n; extra > 0 {
}
}
// CloneMapNonNil is like maps.Clone except it can't return nil, it will return an empty map instead.
m = maps.Clone(m)
if m == nil {
}
}
// InverseMap creates the inverse map, ie., for a key-value map, it builds the value-key map.
if m == nil {
return nil
}
for k, v := range m {
}
}
// GetOrSetNew looks up k in m and returns the result. If it's not present, it uses `new` to
// allocate an new value type and sets that in the map, then returns it.
if v, ok := m[k]; ok {
}
m[k] = v
return v
}
// GetOrSetMap looks up k in m, a two-level map, and returns the result. If it's not present,
// it uses `make` to allocate new second-level map and sets that in the first map, then returns it.
if m2, ok := m[k]; ok {
}
m[k] = m2
return m2
}
// DeleteFromMap deletes k2 from the nested map m[k]. If the inner map becomes empty
// after deletion, k is also removed from m to prevent memory leaks.
func DeleteFromMap[M ~map[K]M2, M2 ~map[K2]V, K, K2 comparable, V any](m M, k K, k2 K2) {
util.go ×1
if m2, ok := m[k]; ok {
if len(m2) == 0 {
}
}
}
// MapConcurrent concurrently maps a function over input and fails fast on error.
func MapConcurrent[IN any, OUT any](input []IN, mapper func(IN) (OUT, error)) ([]OUT, error) {
errorsCh := make(chan error, len(input))
results := make([]OUT, len(input))
for i, in := range input {
go func() {
var err error
results[i], err = mapper(in)
errorsCh <- err
}()
}
for range input {
if err := <-errorsCh; err != nil {
return nil, err
}
}
return results, nil
}
// MapSlice given slice xs []T and f(T) S produces slice []S by applying f to every element of xs
if xs == nil {
}
for i, s := range xs {
}
}
// FilterSlice iterates over elements of a slice, returning a new slice of all elements predicate returns true for.
var out []T
for _, elem := range in {
}
}
}
// FoldSlice folds left a slice using given reducer function and initial value.
func FoldSlice[T any, A any](in []T, initializer A, reducer func(A, T) A) A {
acc := initializer
for _, val := range in {
acc = reducer(acc, val)
}
return acc
}
// RepeatSlice given slice and a number (n) produces a new slice containing original slice n times
// if n is non-positive will produce nil
if xs == nil || n <= 0 {
}
for i := range n {
copy(ys[i*len(xs):], xs)
}
return ys
}
// Ptr returns a pointer to a copy of v.
//
//go:fix inline
func Ptr[T any](v T) *T {
return new(v)
}
// InterruptibleSleep is like time.Sleep but can be interrupted by a context.
// Returns context error if interrupted, otherwise nil.
timer := time.NewTimer(timeout)
defer timer.Stop()
select {
return nil
return ctx.Err()
}
}