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 ¶
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 ¶
NewPositive wraps v; a nil v is replaced by a static def.
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.