Documentation
¶
Overview ¶
Package util provides shared utility functions.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BytesToTime ¶
BytesToTime converts bytes back to time.Time. Returns zero time if the input is not exactly 8 bytes.
func StartCleanupLoop ¶
func StartCleanupLoop(ctx context.Context, interval time.Duration, cleanup CleanupFunc, component string, onPanic PanicHandler)
StartCleanupLoop runs cleanup periodically until ctx is canceled. Every invocation is wrapped in recover() so one bad iteration does NOT crash the fred process. On panic, logs the panic + stack with the component label and invokes onPanic (if non-nil) for metric bumping. The loop continues on the next tick regardless.
The "cleanup loop keeps fred alive" invariant is unconditional: a panic inside onPanic itself is also recovered (and logged), so a buggy observability hook can't take down the loop goroutine.
Example usage:
wg.Go(func() {
util.StartCleanupLoop(ctx, interval, cleanupFn, "token",
func(any) { metrics.CleanupPanicsTotal.WithLabelValues("token").Inc() })
})
func TimeToBytes ¶
TimeToBytes converts a time.Time to bytes for storage. Uses big-endian encoding of UnixNano for consistent ordering.
Types ¶
type CleanupFunc ¶
type CleanupFunc func() error
CleanupFunc performs cleanup and returns an error if it fails.
type PanicHandler ¶
type PanicHandler func(recovered any)
PanicHandler is called (from the loop goroutine's recover) when cleanup panics. The caller passes its own trusted handler — typically a closure that bumps a component-specific Prometheus counter. Keeping the handler a per-call parameter (instead of a package global) means: no data race to guard against, no public injection point for untrusted code, and each call site makes its own observability wiring explicit.