Documentation
¶
Overview ¶
Package util provides shared helper functions used across drivers, transports, and the rule engine.
Index ¶
- func ApplyTransform(value any, scale, offset float64) any
- func GetBoolSetting(settings map[string]any, key string, defaultVal bool) bool
- func GetDurationSetting(settings map[string]any, key string, defaultVal time.Duration) time.Duration
- func GetIntSetting(settings map[string]any, key string, defaultVal int) int
- func GetStringSetting(settings map[string]any, key, defaultVal string) string
- func IsConnectionError(err error) bool
- func ReconnectLoopWithBreaker(ctx context.Context, name string, connect func() error, ...)
- func ReconnectLoopWithBreakerCounted(ctx context.Context, name string, connect func() error, ...)
- func ToFloat32(v any) float32
- func ToFloat64(v any) float64
- func ToUint16(v any) uint16
- func ToUint32(v any) uint32
- func ToUint64(v any) uint64
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ApplyTransform ¶
ApplyTransform applies scale and offset to a numeric value. If scale and offset are both 0, the value is returned unchanged.
func GetBoolSetting ¶
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 ¶
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
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 ¶
IsConnectionError reports whether err looks like a connection-lost error by checking for common keywords in its message.
func ReconnectLoopWithBreaker ¶ added in v0.0.2
func ReconnectLoopWithBreaker(ctx context.Context, name string, connect func() error, initialBackoff, 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.
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 ToFloat64 ¶
ToFloat64 converts any numeric value (including bool) to float64. Non-numeric values return 0. bool is treated as 1 (true) / 0 (false).
Types ¶
This section is empty.