Documentation
¶
Overview ¶
Package util provides shared utility functions.
Index ¶
- func BytesToTime(b []byte) time.Time
- func IsNilInterface(value any) bool
- func RunCleanupIteration(cleanup CleanupFunc, component string, onPanic PanicHandler)
- func StartCleanupLoop(ctx context.Context, interval time.Duration, cleanup CleanupFunc, ...)
- func TimeToBytes(t time.Time) []byte
- type CleanupFunc
- type PanicHandler
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 IsNilInterface ¶ added in v0.14.0
IsNilInterface reports whether value is nil or contains a typed nil in one of Go's nil-able kinds. Constructors use it at capability boundaries so an interface that appears non-nil cannot panic when first invoked.
func RunCleanupIteration ¶ added in v0.14.0
func RunCleanupIteration(cleanup CleanupFunc, component string, onPanic PanicHandler)
RunCleanupIteration contains one background iteration, including the optional immediate first pass of a periodic worker, within the same panic boundary. Ordinary errors and panics are logged; onPanic observes only panics.
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) { background.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.