config

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2026 License: BSD-3-Clause Imports: 15 Imported by: 0

Documentation

Overview

Package config provides a tiny loader that populates a configuration struct from multiple sources with clear precedence:

  1. YAML file (if provided via WithYAML)
  2. Environment variables (optionally prefixed via WithEnvPrefix)
  3. Flags from a flag.FlagSet (default flag.CommandLine unless overridden via WithFlagSet)

Later sources override earlier ones (flags > env > YAML)

Index

Constants

This section is empty.

Variables

View Source
var (
	RenewInterval = 1 * time.Minute
)

Functions

func Load

func Load(cfg any, opts ...Option) error

Load populates cfg by merging configuration from (in order): YAML (if provided), environment variables, and flags

Types

type Option

type Option func(*loader)

Option configures the loader used by Load Helpers (WithFlagSet, WithYAML, and WithEnvPrefix) can be used to customize how configuration sources are discovered and merged

func WithEnvPrefix

func WithEnvPrefix(p string) Option

WithEnvPrefix sets a prefix for all environment variables WithEnvPrefix("APP") causes "HOST" to be read from "APP_HOST"

func WithFlagSet

func WithFlagSet(fs *flag.FlagSet) Option

WithFlagSet sets the flag.FlagSet that will be consulted for overrides Flags are evaluated last, giving them the highest precedence over environment variables and YAML values

func WithYAML

func WithYAML(path string) Option

WithYAML specifies the path to a YAML file to load first (lowest precedence) If the file is missing, it is ignored

type Rotator

type Rotator struct {
	// contains filtered or unexported fields
}

Rotator holds the default and current configuration and allows atomic updates. need dynamically update config in a thread-safe manner for all p2p nodes in the cluster

func NewConfigRotator

func NewConfigRotator(
	ctx context.Context,
	log logger.AppLogger,
	baseOptCfg *entities.OptimumConfig,
	chainID,
	clusterID string,
	updater func(config *entities.DynamicConfig),
	opts ...RotatorOption,
) *Rotator

NewConfigRotator creates a new config rotator that periodically fetches and applies dynamic configuration from a remote endpoint. The rotator starts a background goroutine that fetches config at the specified interval. If chainID or clusterID is empty, fetching is disabled. If WithServiceVersion is provided, it is sent as the service_version query parameter. The updater function is called whenever a new config is successfully fetched and applied.

func (*Rotator) Get

func (r *Rotator) Get() *entities.OptimumConfig

Get returns the current configuration. Thread-safe: uses atomic operations for concurrent access.

func (*Rotator) RenewConfig

func (r *Rotator) RenewConfig(cfg *entities.DynamicConfig)

RenewConfig atomically updates the current configuration by applying the dynamic config. Thread-safe: uses atomic operations for concurrent access.

type RotatorOption

type RotatorOption func(*Rotator)

RotatorOption configures a Rotator (e.g. WithRenewInterval).

func WithBootstrapBaseURL

func WithBootstrapBaseURL(baseURL string) RotatorOption

WithBootstrapBaseURL overrides the default production bootstrap endpoint.

func WithRenewInterval

func WithRenewInterval(d time.Duration) RotatorOption

WithRenewInterval sets the interval between config fetch attempts. If not set, RenewInterval is used.

func WithServiceVersion

func WithServiceVersion(v string) RotatorOption

WithServiceVersion sets the optional service version used when fetching dynamic config.

Jump to

Keyboard shortcuts

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