Help us shape the future of Go! Take the 2026 Go Developer Survey

utils

package
v2.17.1 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2026 License: BSD-3-Clause Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	FilePerm os.FileMode = 0o644
	DirPerm  os.FileMode = 0o755
)

Functions

func ActivityBatchIDFromContext added in v2.5.0

func ActivityBatchIDFromContext(ctx context.Context) string

ActivityBatchIDFromContext returns the batch ID attached by WithActivityBatchID, or "" when none is set.

func ActivityRuntimeContext

func ActivityRuntimeContext(requestCtx, appCtx context.Context) context.Context

ActivityRuntimeContext returns a context suitable for activity-backed work.

func ApplyChanged added in v2.6.0

func ApplyChanged[T comparable](target *T, value mo.Option[T]) bool

ApplyChanged updates target when value is present and differs from the current value.

func ApplyEncrypted added in v2.15.0

func ApplyEncrypted(field *string, plaintext string) (bool, error)

ApplyEncrypted stores plaintext encrypted in field only when it differs from the stored value, so unchanged credentials keep their ciphertext. An empty plaintext clears the field. A stored value that no longer decrypts (for example after a key change) is replaced so the source of truth wins.

func ApplyNullable added in v2.6.0

func ApplyNullable[T comparable](target **T, value mo.Option[T]) bool

ApplyNullable updates target to the optional value when it differs from the current value.

func ApplySliceChanged added in v2.7.0

func ApplySliceChanged[S ~[]E, E comparable](target *S, value mo.Option[S]) bool

ApplySliceChanged updates target when value is present and its elements differ from the current slice. Nil and empty slices are treated as equal.

func ExtractCategoryMetadata

func ExtractCategoryMetadata[T any]() map[string]map[string]string

ExtractCategoryMetadata extracts category metadata from T's fields with catmeta tags Returns a map of category ID to category metadata in field order

func IsAppLifecycleContext

func IsAppLifecycleContext(ctx context.Context) bool

IsAppLifecycleContext reports whether ctx is tied to the application lifecycle.

func LookupEnvOrFile added in v2.15.0

func LookupEnvOrFile(name string) (value string, found, fromFile bool)

LookupEnvOrFile is os.LookupEnv with Docker secret support: when NAME__FILE or NAME_FILE (checked in that order) names a readable file, its trimmed contents stand in for NAME. An unreadable file falls back to NAME. The booleans report whether a value was found and whether it came from a file.

func NormalizePasskeyAssertionExtensions added in v2.14.0

func NormalizePasskeyAssertionExtensions(session protocol.SessionExtensions, assertion *protocol.ParsedCredentialAssertionData)

NormalizePasskeyAssertionExtensions drops an unrequested appid=false output, which Safari reports even though it means the legacy AppID was not used.

func PanicToError added in v2.15.0

func PanicToError(r any) error

PanicToError converts a recovered panic value into an error.

func ParseKeywords

func ParseKeywords(keywordsStr string) []string

ParseKeywords parses a comma-separated keywords string into a slice Returns an empty slice if the input is empty or contains only whitespace

func ParseMetaTag

func ParseMetaTag(tag string) map[string]string

ParseMetaTag parses a struct tag meta value formatted as `k=v;other=val;...` Returns a map of key-value pairs extracted from the tag

func RecoverToError added in v2.6.0

func RecoverToError(errPtr *error, label string, args ...any)

RecoverToError converts a panic in the calling goroutine into an error and logs it. x/sync's errgroup does not recover worker panics — they crash the whole process — so every worker defers this as its first statement:

g.Go(func() (workerErr error) {
	defer utils.RecoverToError(&workerErr, "image list worker")
	...
})

It must be deferred directly (not wrapped in another closure) for recover() to observe the panic. errPtr may be nil when the caller has no error to report into (e.g. a fire-and-forget goroutine); the panic is still logged. args are extra slog attributes appended to the log record.

func UpdateInitiatorFromContext added in v2.14.0

func UpdateInitiatorFromContext(ctx context.Context) (activitytypes.StartedBy, bool)

UpdateInitiatorFromContext returns trusted proxied update attribution.

func ValidActivityBatchID added in v2.5.0

func ValidActivityBatchID(id string) bool

ValidActivityBatchID reports whether id is safe to persist as a batch ID.

func WaitGroup added in v2.7.0

func WaitGroup(ctx context.Context, group *sync.WaitGroup) error

WaitGroup waits for all work or returns when ctx ends. After a timeout, its waiter goroutine remains until the group eventually completes.

func WithActivityBatchID added in v2.5.0

func WithActivityBatchID(ctx context.Context, batchID string) context.Context

WithActivityBatchID attaches a client-supplied batch ID that groups the activities spawned by one logical user action (e.g. a bulk container update). Invalid IDs are ignored.

func WithAppLifecycleContext

func WithAppLifecycleContext(ctx context.Context) context.Context

WithAppLifecycleContext marks ctx as the application lifecycle context.

func WithUpdateInitiator added in v2.14.0

func WithUpdateInitiator(ctx context.Context, initiator activitytypes.StartedBy) context.Context

WithUpdateInitiator carries manager-authenticated attribution through an agent request without changing the authenticated agent identity.

Types

type KeyedMutex added in v2.6.0

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

KeyedMutex hands out one mutex per key, so unrelated keys proceed concurrently while work on the same key is serialized.

Entries are reference counted and dropped once nobody holds or awaits them. The naive version of this — a sync.Map of *sync.Mutex — either grows one entry per key ever seen, or, if entries are deleted on release, hands two callers different mutexes for the same key and silently loses mutual exclusion. The reference count is taken under the map lock, so a key with waiters is never removed.

The zero value is ready to use.

func (*KeyedMutex) Lock added in v2.6.0

func (k *KeyedMutex) Lock(key string) func()

Lock blocks until the key's mutex is held and returns its unlock function. Intended to be deferred at the point of acquisition:

defer locks.Lock(id)()

func (*KeyedMutex) LockContext added in v2.15.0

func (k *KeyedMutex) LockContext(ctx context.Context, key string) (func(), error)

LockContext is Lock bounded by ctx. When ctx ends first the key is left untouched and the waiter is released once the lock would have been taken.

func (*KeyedMutex) RLock added in v2.8.0

func (k *KeyedMutex) RLock(key string) func()

RLock blocks until the key's mutex is held for reading and returns its unlock function. Multiple readers for the same key may proceed concurrently.

func (*KeyedMutex) TryLock added in v2.6.0

func (k *KeyedMutex) TryLock(key string) (func(), bool)

TryLock acquires the key's mutex without blocking. It reports whether the lock was taken; the unlock function is only valid when it was.

type SyncGate added in v2.15.0

type SyncGate struct {
	Expiry time.Duration
	// contains filtered or unexported fields
}

SyncGate remembers the SHA-256 of the last payload delivered per scope and key so periodic re-syncs skip the network when nothing changed, and serializes deliveries for one key so overlapping pushes reach the target in the order they are remembered. Only the digest is retained. Expiry bounds how long a delivery is trusted (zero means forever) so a target rebuilt behind an unchanged status still converges. The zero value is ready to use.

func (*SyncGate) Begin added in v2.15.0

func (g *SyncGate) Begin(ctx context.Context, scope, key string, body []byte) (unchanged bool, finish func(delivered bool), err error)

Begin reports whether body is already delivered for scope and key. When it is not, the key stays locked until finish runs; finish(true) remembers the delivery unless the scope was forgotten in between. Callers must invoke finish exactly once. Waiting for an in-flight delivery ends with ctx.

func (*SyncGate) Forget added in v2.15.0

func (g *SyncGate) Forget(scope string)

Forget drops every remembered payload for scope so the next sync resends, and discards the outcome of deliveries still in flight for it.

Directories

Path Synopsis
Package acfs maps ACFS protocol types to Arcane workspace contracts.
Package acfs maps ACFS protocol types to Arcane workspace contracts.
Package backupbrowser provides deterministic backup-tree construction, paging, and restore-selection validation.
Package backupbrowser provides deterministic backup-tree construction, paging, and restore-selection validation.
Package dbutil provides small generic helpers around GORM that consolidate repetitive single-row lookup and transaction boilerplate found across services.
Package dbutil provides small generic helpers around GORM that consolidate repetitive single-row lookup and transaction boilerplate found across services.
Package francis owns the embedded Francis host lifecycle.
Package francis owns the embedded Francis host lifecycle.
testing
Package testing starts isolated Francis hosts for owning-logic tests.
Package testing starts isolated Francis hosts for owning-logic tests.
Package iconcatalog resolves Arcane icon metadata into catalog URLs.
Package iconcatalog resolves Arcane icon metadata into catalog URLs.
Package imageref contains shared image-reference parsing operations.
Package imageref contains shared image-reference parsing operations.
Package s3 provides reusable S3 configuration and connectivity helpers.
Package s3 provides reusable S3 configuration and connectivity helpers.
Package tracing holds shared helpers for Arcane's own OpenTelemetry spans.
Package tracing holds shared helpers for Arcane's own OpenTelemetry spans.

Jump to

Keyboard shortcuts

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