utils

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2026 License: MIT Imports: 31 Imported by: 21

Documentation

Index

Constants

View Source
const (

	// DefaultVersion is the image tag assumed when a reference carries none.
	DefaultVersion = "latest"
)

Variables

This section is empty.

Functions

func AdvancedDivide

func AdvancedDivide[T numbers](a, b T) T

AdvancedDivide returns 0 when either operand is 0.

func Bool2Int

func Bool2Int(a bool) int

func CleanStatsdMetrics

func CleanStatsdMetrics(k string) string

CleanStatsdMetrics replaces "." with "-".

func DecodeMetaInLabel

func DecodeMetaInLabel(ctx context.Context, labels map[string]string) *types.LabelMeta

DecodeMetaInLabel returns the meta from labels, or an empty meta when absent or malformed.

func EncodeMetaInLabel

func EncodeMetaInLabel(ctx context.Context, meta *types.LabelMeta) string

EncodeMetaInLabel returns meta as JSON, or "" when marshaling fails.

func EncodePublishInfo

func EncodePublishInfo(info map[string][]string) map[string]string

EncodePublishInfo joins each network's addresses with commas.

func EnsureReaderClosed

func EnsureReaderClosed(ctx context.Context, stream io.ReadCloser)

EnsureReaderClosed drains stream to EOF and closes it; a nil stream is a no-op.

func GetGitRepoName

func GetGitRepoName(url string) (string, error)

func GetHTTPClient

func GetHTTPClient() *http.Client

func GetHTTPSClient

func GetHTTPSClient(ctx context.Context, certPath, name, ca, cert, key string) (*http.Client, error)

GetHTTPSClient returns a per-cert cached HTTPS client, or the plain HTTP client when any of certPath/ca/cert/key is empty.

func GetOutboundAddress

func GetOutboundAddress(bind, probeTarget string) (string, error)

GetOutboundAddress returns bind, or the local IP reaching probeTarget when bind's host is unspecified.

func GetUnixSockClient

func GetUnixSockClient() *http.Client

func LabelsFilter

func LabelsFilter(extend, labels map[string]string) bool

LabelsFilter reports whether every key/value in labels is present in extend.

func ListAllExecutableFiles

func ListAllExecutableFiles(basedir string) ([]string, error)

ListAllExecutableFiles returns the executable files directly under basedir, not recursing.

func LoadConfig

func LoadConfig(configPath string) (types.Config, error)

LoadConfig loads the config from the YAML file at configPath.

func MakeCommandLineArgs

func MakeCommandLineArgs(s string) []string

MakeCommandLineArgs splits s into argv, honoring single and double quotes.

func MakePublishInfo

func MakePublishInfo(networks map[string]string, ports []string) map[string][]string

MakePublishInfo maps each network to its "ip:port" strings.

func MakeWorkloadName

func MakeWorkloadName(appname, entrypoint, ident string) string

MakeWorkloadName joins appname, entrypoint, ident using '_'

func Map

func Map[T1, T2 any](slice []T1, f func(T1) T2) []T2

Map returns f applied to every element of slice.

func MergeHookOutputs

func MergeHookOutputs(outputs []*bytes.Buffer) []byte

func NewInheritCtx

func NewInheritCtx(ctx context.Context) context.Context

NewInheritCtx returns ctx with its values but without its cancellation.

func NewPool

func NewPool(max int) (*ants.PoolWithFunc, error)

NewPool returns a blocking pool; a full pool makes Invoke wait for a free worker.

func PCR

func PCR(ctx context.Context, prepare, commit, rollback contextFunc, ttl time.Duration) error

PCR runs prepare, commit and rollback; prepare must be side-effect free.

func ParseRAMInHuman

func ParseRAMInHuman(ram string) (int64, error)

ParseRAMInHuman parses a human-readable size ("100KB", "-1T") into bytes.

func ParseWorkloadName

func ParseWorkloadName(workloadName string) (string, string, string, error)

ParseWorkloadName is the inverse of MakeWorkloadName.

func RandomString

func RandomString(n int) string

RandomString returns n random letters from [a-zA-Z].

func Range

func Range(n int) []int

Range returns []int{0, 1, ..., n-1}.

func Round

func Round(f float64) float64

Round rounds f to 9 decimal places.

func SHA256

func SHA256(input string) string

func SentryGo

func SentryGo(f func())

SentryGo runs f in a goroutine and reports a panic to Sentry before re-raising it.

func ShortID

func ShortID(workloadID string) string

ShortID returns the last 7 characters of workloadID.

func Tail

func Tail(path string) string

Tail returns the segment of path after the last "/".

func TempFile

func TempFile(stream io.ReadCloser) (name string, err error)

TempFile copies stream into a new temp file, closes stream, and returns the file name.

func Txn

func Txn(ctx context.Context, cond, then contextFunc, rollback rollbackFunc, ttl time.Duration) (txnErr error)

Txn runs cond then then; on any error it runs rollback under a fresh ttl-bounded context.

func WithTimeout

func WithTimeout(ctx context.Context, timeout time.Duration, f func(context.Context))

WithTimeout runs a function with given timeout

Types

type Statsd

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

Statsd batches metrics into UDP datagrams and drops send failures.

func NewStatsd

func NewStatsd(addr string) (*Statsd, error)

NewStatsd dials the statsd server at addr and flushes buffered metrics every 100ms.

func (*Statsd) Close

func (s *Statsd) Close() error

Close flushes the buffer, stops the flush ticker and releases the connection.

func (*Statsd) Count

func (s *Statsd) Count(name string, n int, rate float32)

Count adds n to name, sampling at rate.

func (*Statsd) Gauge

func (s *Statsd) Gauge(name string, value float64)

Gauge sets name to value, prefixed by a zero when value is negative because statsd reads a leading minus as a delta.

Jump to

Keyboard shortcuts

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