source

package
v1.8.2 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package source provides a composable configuration source system. Sources are checked in priority order; the first hit wins.

Index

Constants

This section is empty.

Variables

View Source
var ErrKeyNotFound = errors.New("source: key not found")

ErrKeyNotFound is returned when no source contains the requested key.

Functions

This section is empty.

Types

type Chain

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

Chain is a priority-ordered list of Sources. Get returns the first hit.

func NewChain

func NewChain(sources ...Source) *Chain

NewChain creates a new priority chain from sources (first = highest priority).

func (*Chain) Close

func (c *Chain) Close() error

Close closes all sources.

func (*Chain) Get

func (c *Chain) Get(ctx context.Context, key string) (*Value, error)

Get returns the value from the first source that contains the key.

func (*Chain) List

func (c *Chain) List(ctx context.Context, prefix string) ([]*Value, error)

List merges values from all sources. Higher-priority sources override lower ones.

func (*Chain) Watch

func (c *Chain) Watch(ctx context.Context, key string, fn WatchFunc) error

Watch registers watchers on all sources for the given key.

type Database

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

Database reads configuration from a config.Store backend.

func NewDatabase

func NewDatabase(store config.Store, appID string, opts ...DatabaseOption) *Database

NewDatabase creates a Database source backed by the given config store.

func (*Database) Close

func (d *Database) Close() error

Close stops the poll goroutine and releases resources.

func (*Database) Get

func (d *Database) Get(ctx context.Context, key string) (*Value, error)

Get retrieves a config entry and wraps it as a Value.

func (*Database) List

func (d *Database) List(ctx context.Context, prefix string) ([]*Value, error)

List returns all config entries for the app matching the prefix.

func (*Database) Name

func (d *Database) Name() string

Name returns "database".

func (*Database) Watch

func (d *Database) Watch(ctx context.Context, key string, fn WatchFunc) error

Watch registers a callback for changes to a key. The database source uses polling to detect changes, starting a background goroutine on the first registration. Watch is non-blocking and returns immediately.

type DatabaseOption

type DatabaseOption func(*Database)

DatabaseOption configures a Database source.

func WithMetrics

func WithMetrics(m gometrics.Metrics) DatabaseOption

WithMetrics counts the source's watch polls on m by result: changed, unchanged or error. A failed read is otherwise silent: the poll skips the key until the next tick. A nil m records nothing.

func WithPollInterval

func WithPollInterval(d time.Duration) DatabaseOption

WithPollInterval sets the polling interval for watch. Default is 30 seconds.

type Env

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

Env reads configuration from environment variables.

func NewEnv

func NewEnv(prefix string) *Env

NewEnv creates an Env source. If prefix is non-empty, keys are looked up as PREFIX_KEY (uppercased, dashes replaced with underscores).

func (*Env) Close

func (e *Env) Close() error

Close is a no-op.

func (*Env) Get

func (e *Env) Get(_ context.Context, key string) (*Value, error)

Get reads the key from os.Getenv. Keys are normalized: uppercased, dashes → underscores.

func (*Env) List

func (e *Env) List(_ context.Context, _ string) ([]*Value, error)

List is not supported for environment variables; returns nil.

func (*Env) Name

func (e *Env) Name() string

Name returns "env".

func (*Env) Watch

func (e *Env) Watch(_ context.Context, _ string, _ WatchFunc) error

Watch is not supported for environment variables; returns nil immediately.

type Memory

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

Memory is an in-memory source useful for testing and dynamic overrides.

func NewMemory

func NewMemory() *Memory

NewMemory creates a new empty Memory source.

func (*Memory) Close

func (m *Memory) Close() error

Close is a no-op.

func (*Memory) Get

func (m *Memory) Get(_ context.Context, key string) (*Value, error)

Get retrieves a value by key.

func (*Memory) List

func (m *Memory) List(_ context.Context, prefix string) ([]*Value, error)

List returns all values whose keys have the given prefix.

func (*Memory) Name

func (m *Memory) Name() string

Name returns "memory".

func (*Memory) Set

func (m *Memory) Set(ctx context.Context, key, raw string)

Set sets a value and notifies watchers.

func (*Memory) Watch

func (m *Memory) Watch(_ context.Context, key string, fn WatchFunc) error

Watch registers a callback for the given key. Non-blocking — returns immediately.

type Source

type Source interface {
	// Name returns a human-readable name for this source.
	Name() string

	// Get retrieves a value by key.
	Get(ctx context.Context, key string) (*Value, error)

	// List returns all values matching a prefix (empty prefix = all).
	List(ctx context.Context, prefix string) ([]*Value, error)

	// Watch registers a callback for changes to a key. It blocks until ctx is done.
	// Sources that don't support watching should return nil immediately.
	Watch(ctx context.Context, key string, fn WatchFunc) error

	// Close releases any resources held by the source.
	Close() error
}

Source is a configuration value provider.

type Value

type Value struct {
	Key       string            `json:"key"`
	Raw       string            `json:"raw"`
	Source    string            `json:"source"`
	Version   int64             `json:"version,omitempty"`
	ExpiresAt *time.Time        `json:"expires_at,omitempty"`
	Metadata  map[string]string `json:"metadata,omitempty"`
}

Value represents a configuration value from a source.

type WatchFunc

type WatchFunc func(ctx context.Context, key string, val *Value)

WatchFunc is called when a source value changes.

Jump to

Keyboard shortcuts

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