utils

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2026 License: MIT Imports: 26 Imported by: 21

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AdvancedDivide

func AdvancedDivide(a, b float64) float64

AdvancedDivide returns 0 instead of dividing by zero.

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 GetOutboundAddress

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

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

func KeepAlive added in v0.1.1

func KeepAlive(ctx context.Context, interval time.Duration, refresh func(context.Context) error) error

KeepAlive calls refresh every interval until ctx ends or refresh fails, returning the failure.

func LabelsFilter

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

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

func LastEnvValue added in v0.1.1

func LastEnvValue(env []string, key string) string

LastEnvValue takes the last value of key, as core appends its own after the client's.

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 NewHealthCheck added in v0.1.1

func NewHealthCheck(check *types.HealthCheck) *enginetypes.HealthCheck

NewHealthCheck renders the label's health check the way a workload record carries it.

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 ParseRate added in v0.1.1

func ParseRate(rate string) int64

ParseRate reads a human byte rate, treating junk and negatives as zero.

func ParseWorkloadName

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

ParseWorkloadName is the inverse of MakeWorkloadName.

func RandomID added in v0.1.1

func RandomID() string

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 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 Txn

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

Txn runs cond then then, rolling back under a fresh ttl-bounded context on error; settled reports full compensation.

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.

type VolumeBind added in v0.1.1

type VolumeBind struct {
	Source   string
	Dest     string
	ReadOnly bool
}

VolumeBind is one host bind parsed from "source:dest[:ro]" after env expansion.

func ParseVolumeBinds added in v0.1.1

func ParseVolumeBinds(volumes, env []string) []VolumeBind

ParseVolumeBinds keeps the well-formed binds and expands $VAR from env.

Jump to

Keyboard shortcuts

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