util

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2026 License: MIT Imports: 6 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package util provides shared helper functions used across drivers, transports, and the rule engine.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyTransform

func ApplyTransform(value any, scale, offset float64) any

ApplyTransform applies scale and offset to a numeric value. If scale and offset are both 0, the value is returned unchanged.

func GetBoolSetting

func GetBoolSetting(settings map[string]any, key string, defaultVal bool) bool

GetBoolSetting extracts a bool from a settings map, falling back to defaultVal if the key is missing or the value is not a bool.

func GetDurationSetting

func GetDurationSetting(settings map[string]any, key string, defaultVal time.Duration) time.Duration

GetDurationSetting extracts a duration string (e.g. "5s") from a settings map, falling back to defaultVal if missing or unparseable.

func GetIntSetting

func GetIntSetting(settings map[string]any, key string, defaultVal int) int

GetIntSetting extracts an int from a settings map, falling back to defaultVal if the key is missing or the value is not numeric. YAML unmarshalling may produce int, uint64, int64, or float64 depending on the parser and value magnitude, so all numeric kinds are accepted.

func GetStringSetting added in v0.0.3

func GetStringSetting(settings map[string]any, key, defaultVal string) string

GetStringSetting extracts a string from a settings map, falling back to defaultVal if the key is missing or the value is not a string. YAML unmarshalling produces a string for quoted scalar values, so only the string kind is accepted; other types fall back to defaultVal.

func IsConnectionError

func IsConnectionError(err error) bool

IsConnectionError reports whether err looks like a connection-lost error by checking for common keywords in its message.

func ReconnectLoopOpts added in v0.0.4

func ReconnectLoopOpts(ctx context.Context, name string, connect func() error, opts ReconnectOpts, counter *atomic.Uint64)

ReconnectLoopOpts is the opts-struct variant of ReconnectLoopWithBreakerCounted. It collapses the 7 positional parameters into a context, name, connect func, opts struct, and optional counter — keeping the call site readable.

func ReconnectLoopWithBreaker added in v0.0.2

func ReconnectLoopWithBreaker(ctx context.Context, name string, connect func() error, initialBackoff, maxBackoff time.Duration, maxFailures int)

ReconnectLoopWithBreaker behaves like ReconnectLoopWithBreakerCounted but does not track reconnect attempts. Use the counted variant when a driver wants to expose a reconnect Prometheus metric.

Backoff starts at initialBackoff and doubles after each failure, capped at maxBackoff. A circuit-breaker trips after maxFailures consecutive failures (maxFailures > 0) and increases the backoff to 5 minutes. The breaker resets on the next successful connection. maxFailures <= 0 disables the breaker (pure exponential backoff).

func ReconnectLoopWithBreakerCounted added in v0.0.3

func ReconnectLoopWithBreakerCounted(ctx context.Context, name string, connect func() error, initialBackoff, maxBackoff time.Duration, maxFailures int, counter *atomic.Uint64)

ReconnectLoopWithBreakerCounted behaves like ReconnectLoopWithBreaker and additionally increments counter (when non-nil) at the start of each reconnect attempt — whether that attempt ultimately succeeds or fails. Drivers pass an atomic.Uint64 held on the driver struct so the value can be surfaced via Status() and exposed as a Prometheus counter.

The counter is incremented before connect() is invoked, so even if the caller cancels the context mid-attempt, every attempt that began is reflected in the count. A nil counter disables counting and makes this equivalent to ReconnectLoopWithBreaker.

func ToFloat32

func ToFloat32(v any) float32

ToFloat32 converts any numeric value to float32. Non-numeric returns 0.

func ToFloat64

func ToFloat64(v any) float64

ToFloat64 converts any numeric value (including bool) to float64. Non-numeric values return 0. bool is treated as 1 (true) / 0 (false).

func ToFloat64OK added in v0.0.4

func ToFloat64OK(v any) (float64, bool)

ToFloat64OK converts any numeric value to float64 and returns true. Non-numeric values (bool, string, nil, etc.) return 0 and false. Unlike ToFloat64, bool is NOT treated as numeric — callers that need to reject non-numeric values (e.g. transform expressions) should use this.

func ToUint16

func ToUint16(v any) uint16

ToUint16 converts a numeric any value to uint16.

func ToUint32

func ToUint32(v any) uint32

ToUint32 converts any numeric value to uint32. Non-numeric returns 0.

func ToUint64

func ToUint64(v any) uint64

ToUint64 converts any numeric value to uint64. Non-numeric returns 0.

Types

type ReconnectOpts added in v0.0.4

type ReconnectOpts struct {
	InitialBackoff time.Duration
	MaxBackoff     time.Duration
	MaxFailures    int
}

ReconnectLoopWithBreaker repeatedly calls connect with exponential backoff until it succeeds or ctx is cancelled. The backoff starts at initialBackoff and doubles on each failure, capped at maxBackoff. <=0 values fall back to 2s initial and 30s max.

A ±20% random jitter is applied to every wait so that when the network recovers, drivers that disconnected simultaneously do not all reconnect at once (thundering herd). The base backoff progression (exponential doubling and the circuit-breaker interval) is preserved; only the actual sleep duration is jittered.

After maxFailures consecutive failures (maxFailures > 0), a circuit breaker trips and the backoff is increased to circuitBreakerBackoff (5 minutes) to avoid hammering a permanently offline device. The breaker resets on the next successful connection. maxFailures <= 0 disables the breaker (pure exponential backoff).

It is a thin wrapper around ReconnectLoopWithBreakerCounted that does not track reconnect attempts. Use the counted variant when a driver wants to expose a reconnect Prometheus metric. ReconnectOpts bundles the parameters for reconnect loop configuration. Use it with ReconnectLoopOpts to avoid passing 6+ positional arguments (taste invariant T4: parameter count ≤ 5).

Jump to

Keyboard shortcuts

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