xvalue

package
v0.12.2 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package xvalue provides configuration values that may be static or change at runtime.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DynamicValue

type DynamicValue[T any] struct {
	// contains filtered or unexported fields
}

DynamicValue is a Value backed by a user-provided fetch function that is polled in the background. Get always returns the cached value and never performs I/O.

func NewDynamicValue

func NewDynamicValue[T any](
	ctx context.Context,
	defaultValue T,
	fetch func(ctx context.Context) (T, error),
	interval time.Duration,
) *DynamicValue[T]

NewDynamicValue creates a Value that polls fetch every interval and caches the result.

The first fetch is performed synchronously; if it fails, defaultValue is cached. When a later fetch fails, the last successful value is kept. Every fetch call receives a context derived from ctx with a timeout equal to interval, and fetch must honor it: all time bounds assume fetch returns promptly once its ctx is done. A panic inside fetch is recovered and treated as a fetch error.

Polling runs in a goroutine that lives until ctx is done; there is no Stop method. Using context.Background() keeps that goroutine for the process lifetime. Fetch errors are logged using ctx, so attach identifying fields to it via xlog.

Misconfigured arguments (nil ctx, nil fetch, non-positive interval) do not fail: a warning is logged and the value stays at defaultValue, like a static one.

func (*DynamicValue[T]) Get

func (v *DynamicValue[T]) Get() T

Get returns the last successfully fetched value (or the default).

type Number

type Number interface {
	~int | ~int32 | ~int64
}

Number is the set of value types the Positive validator accepts (time.Duration included).

type Positive

type Positive[T Number] struct {
	// contains filtered or unexported fields
}

Positive guards a Value that must stay > 0. An invalid value is never applied: the last valid value (or the default) is returned instead and a warning is logged.

func NewPositive

func NewPositive[T Number](name string, v Value[T], def T) *Positive[T]

NewPositive wraps v; a nil v is replaced by a static def.

func (*Positive[T]) Load

func (p *Positive[T]) Load(ctx context.Context) T

Load returns the current value if it is valid, otherwise the last valid one. A Get that panics (e.g. a typed-nil Value) counts as invalid. ctx is used only for logging.

type StaticValue

type StaticValue[T any] struct {
	// contains filtered or unexported fields
}

StaticValue is a Value that never changes.

func NewStaticValue

func NewStaticValue[T any](v T) *StaticValue[T]

NewStaticValue creates a Value that always returns v.

func (*StaticValue[T]) Get

func (v *StaticValue[T]) Get() T

Get returns the static value.

type Value

type Value[T any] interface {
	// Get returns the current value. It must be non-blocking, must not panic
	// and must be safe for concurrent use: processors call it on the hot path.
	Get() T
}

Value provides a configuration value that may change over time.

Jump to

Keyboard shortcuts

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