package helper

import "github.com/hashicorp/nomad/helper"

These functions are coming from consul/lib/eof.go

Index

Functions

func Backoff

func Backoff(backoffBase time.Duration, backoffLimit time.Duration, attempt uint64) time.Duration

func CheckHCLKeys

func CheckHCLKeys(node ast.Node, valid []string) error

func CheckNamespaceScope

func CheckNamespaceScope(provided string, requested []string) []string

CheckNamespaceScope ensures that the provided namespace is equal to or a parent of the requested namespaces. Returns requested namespaces which are not equal to or a child of the provided namespace.

func CleanEnvVar

func CleanEnvVar(s string, r byte) string

CleanEnvVar replaces all occurrences of illegal characters in an environment variable with the specified byte.

func CleanFilename

func CleanFilename(filename string, replace string) string

CleanFilename replaces invalid characters in filename

func CleanFilenameASCIIOnly

func CleanFilenameASCIIOnly(filename string, replace string) string

CleanFilenameASCIIOnly replaces invalid and non-ASCII characters in filename

func CleanFilenameStrict

func CleanFilenameStrict(filename string, replace string) string

CleanFilenameStrict replaces invalid and punctuation characters in filename

func ConvertMap

func ConvertMap[K comparable, A, B any](original map[K]A, conversion func(a A) B) map[K]B

ConvertMap takes the input map and generates a new one using the supplied conversion function to convert the values. This is useful when converting one map to another using the same keys.

func ConvertSlice

func ConvertSlice[A, B any](original []A, conversion func(a A) B) []B

ConvertSlice takes the input slice and generates a new one using the supplied conversion function to covert the element. This is useful when converting a slice of strings to a slice of structs which wraps the string.

func CopyMapOfSlice

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

CopyMapOfSlice creates a copy of m, making copies of each []V.

func CopySlice

func CopySlice[S ~[]E, E Copyable[E]](s S) S

CopySlice creates a deep copy of s. For slices with elements that do not implement Copy(), use slices.Clone.

func DeepCopyMap

func DeepCopyMap[M ~map[K]V, K comparable, V Copyable[V]](m M) M

DeepCopyMap creates a copy of m by calling Copy() on each value.

If m is nil the return value is nil.

func ElementsEqual

func ElementsEqual[T EqualFunc[T]](a, b []T) bool

ElementsEqual returns true if slices a and b contain the same elements (in no particular order) using the Equal function defined on their type for comparison.

func ExpiryToRenewTime

func ExpiryToRenewTime(exp time.Time, now func() time.Time, minWait time.Duration) time.Duration

ExpiryToRenewTime calculates how long until clients should try to renew credentials based on their expiration time and now.

Renewals will begin halfway between now and the expiry plus some jitter.

If the expiration is in the past or less than the min wait, then the min wait time will be used with jitter.

func FindExecutableFiles

func FindExecutableFiles(path string) (map[string]string, error)

FindExecutableFiles looks in the provided path for executables and returns a map where keys are filenames and values are the absolute path.

func FlattenMultierror

func FlattenMultierror(err error) error

FlattenMultierror takes a multierror and unwraps it if there's only one error in the output, otherwise returning the multierror or nil.

func HashUUID

func HashUUID(input string) (output string, hashed bool)

HashUUID takes an input UUID and returns a hashed version of the UUID to ensure it is well distributed.

func IsDisjoint

func IsDisjoint[T comparable](first, second []T) (bool, []T)

IsDisjoint returns whether first and second are disjoint sets, and the set of offending elements if not.

func IsErrEOF

func IsErrEOF(err error) bool

IsErrEOF returns true if we get an EOF error from the socket itself, or an EOF equivalent error from yamux.

func IsExecutable

func IsExecutable(i os.FileInfo) bool

func IsMethodHTTP

func IsMethodHTTP(s string) bool

IsMethodHTTP returns whether s is a known HTTP method, ignoring case.

func IsSubset

func IsSubset[T comparable](larger, smaller []T) (bool, []T)

IsSubset returns whether the smaller set of items is a subset of the larger. If the smaller set is not a subset, the offending elements are returned.

func IsUUID

func IsUUID(str string) bool

IsUUID returns true if the given string is a valid UUID.

func IsValidInterpVariable

func IsValidInterpVariable(str string) bool

IsValidInterpVariable returns true if a valid dotted variable names for interpolation. The string must begin with one or more non-dot characters which may be followed by sequences containing a dot followed by a one or more non-dot characters.

func Merge

func Merge[T comparable](a, b T) T

Merge takes two variables and returns variable b in case a has zero value. For pointer values please use pointer.Merge.

func MergeMapStringString

func MergeMapStringString(m map[string]string, n map[string]string) map[string]string

MergeMapStringString will merge two maps into one. If a duplicate key exists the value in the second map will replace the value in the first map. If both maps are empty or nil this returns an empty map.

func MergeMultierrorWarnings

func MergeMultierrorWarnings(errs ...error) string

MergeMultierrorWarnings takes warnings and merges them into a returnable string. This method is used to return API and CLI errors.

func OpaqueMapsEqual

func OpaqueMapsEqual[M ~map[K]V, K comparable, V any](m1, m2 M) bool

OpaqueMapsEqual compare maps[<comparable>]<any> for equality, but safely by using the cmp package and ignoring un-exported types, and by treating nil/empty slices and maps as equal.

This is intended as a substitute for reflect.DeepEqual in the case of "opaque maps", e.g. `map[comparable]any` - such as the case for Task Driver config or Envoy proxy pass-through configuration.

func RandomStagger

func RandomStagger(interval time.Duration) time.Duration

RandomStagger returns an interval between 0 and the duration

func RateScaledInterval

func RateScaledInterval(rate float64, min time.Duration, n int) time.Duration

RateScaledInterval is used to choose an interval to perform an action in order to target an aggregate number of actions per second across the whole cluster.

func RemoveEqualFold

func RemoveEqualFold(xs *[]string, search string)

RemoveEqualFold removes the first string that EqualFold matches. It updates xs in place

func SliceSetEq

func SliceSetEq[T comparable](a, b []T) bool

SliceSetEq returns true if slices a and b contain the same elements (in no particular order), using '==' for comparison.

Note: for pointers, consider implementing an Equal method and using ElementsEqual instead.

func SliceToMap

func SliceToMap[M ~map[K]V, K comparable, V any](slice []V, keyFn func(value V) K) M

SliceToMap creates a map from a slice, using keyFn to extract a key from each value. Note this doesn't copy the elements of the slice, so you may need to call slices.Clone on the result

func StringHasPrefixInSlice

func StringHasPrefixInSlice(s string, prefixes []string) bool

StringHasPrefixInSlice returns true if s starts with any prefix in list.

func UniqueMapSliceValues

func UniqueMapSliceValues[K, V comparable](m map[K][]V) []V

UniqueMapSliceValues returns the union of values from each slice in a map[K][]V.

func UnusedKeys

func UnusedKeys(obj interface{}) error

UnusedKeys returns a pretty-printed error if any `hcl:",unusedKeys"` is not empty

func WithBackoffFunc

func WithBackoffFunc(ctx context.Context, minBackoff, maxBackoff time.Duration, fn func() error) error

WithBackoffFunc is a helper that runs a function with geometric backoff + a small jitter to a maximum backoff. It returns once the context closes, with the error wrapping over the error from the function.

func WithLock

func WithLock(lock sync.Locker, f func())

WithLock executes a function while holding a lock.

Types

type Copyable

type Copyable[T any] interface {
	Copy() T
}

type EqualFunc

type EqualFunc[A any] interface {
	Equal(A) bool
}

EqualFunc represents a type implementing the Equal method.

type StopFunc

type StopFunc func()

StopFunc is used to stop a time.Timer created with NewSafeTimer

func NewSafeTicker

func NewSafeTicker(duration time.Duration) (*time.Ticker, StopFunc)

func NewSafeTimer

func NewSafeTimer(duration time.Duration) (*time.Timer, StopFunc)

NewSafeTimer creates a time.Timer but does not panic if duration is <= 0.

Using a time.Timer is recommended instead of time.After when it is necessary to avoid leaking goroutines (e.g. in a select inside a loop).

Returns the time.Timer and also a StopFunc, forcing the caller to deal with stopping the time.Timer to avoid leaking a goroutine.

Note: If creating a Timer that should do nothing until Reset is called, use NewStoppedTimer instead for safely creating the timer in a stopped state.

func NewStoppedTimer

func NewStoppedTimer() (*time.Timer, StopFunc)

NewStoppedTimer creates a time.Timer in a stopped state. This is useful when the actual wait time will computed and set later via Reset.

Source Files

backoff.go cluster.go eof.go funcs.go opaque.go retry.go warning.go

Directories

PathSynopsis
helper/args
helper/boltddPackage boltdd contains a wrapper around BBoltDB to deduplicate writes and encode values using mgspack.
helper/broker
helper/bufconndialer
helper/codec
helper/constraints
helper/constraints/semversemver is a Semver Constraints package copied from github.com/hashicorp/go-version @ 2046c9d0f0b03c779670f5186a2a4b2c85493a71
helper/crypto
helper/discover
helper/envoyPackage envoy provides a high level view of the variables that go into selecting an envoy version.
helper/escapingfs
helper/escapingio
helper/flags
helper/flatmap
helper/gated-writer
helper/goruntimePackage goruntime contains helper functions related to the Go runtime.
helper/group
helper/grpc-middleware
helper/grpc-middleware/logging
helper/ipaddr
helper/iterator
helper/joseutil
helper/logging
helper/mount
helper/noxssrwPackage noxssrw (No XSS ResponseWriter) behaves like the Go standard library's ResponseWriter by detecting the Content-Type of a response if it has not been explicitly set.
helper/pluginutils
helper/pluginutils/catalogPackage catalog is used to register internal plugins such that they can be loaded.
helper/pluginutils/grpcutils
helper/pluginutils/hclspecutils
helper/pluginutils/hclutils
helper/pluginutils/loader
helper/pluginutils/singleton
helper/pointerPackage pointer provides helper functions related to Go pointers.
helper/pool
helper/raftutilCode generated by go generate; DO NOT EDIT.
helper/safemath
helper/snapshotThe archive utilities manage the internal format of a snapshot, which is a tar file with the following contents:
helper/subprocPackage subproc provides helper utilities for executing the Nomad binary as a child process of the Nomad agent.
helper/testlogPackage testlog creates a *log.Logger backed by *testing.T to ease logging in tests.
helper/testtaskPackage testtask implements a portable set of commands useful as stand-ins for user tasks.
helper/tlsutil
helper/useragent
helper/users
helper/users/dynamicPackage dynamic provides a way of allocating UID/GID to be used by Nomad tasks with no associated service users managed by the operating system.
helper/uuid
helper/winsvc
Version
v1.10.0 (latest)
Published
Apr 9, 2025
Platform
linux/amd64
Imports
24 packages
Last checked
16 minutes ago

Tools for package owners.