dynconfig

package
v0.30.38 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package dynconfig provides a concurrent-safe, file-backed store of runtime settings that can be changed without restarting the server.

Structure

Settings are organised in a two-level namespace: namespace → key → value. Values are JSON scalars or objects; the package enforces well-formedness on every write. Built-in namespaces:

  • "global" — system-wide overrides (e.g. blob.max_bytes)
  • "tenant.{name}" — per-tenant overrides

Any other namespace string is accepted; the two above are conventions.

Persistence

Settings are stored in a single JSON file:

{
  "global": {
    "blob.max_bytes": 104857600
  },
  "tenant.acme": {
    "blob.max_bytes": 10485760
  }
}

The file is reloaded on a configurable interval. Writes go through Set, which updates the in-memory store and flushes to disk atomically.

Well-formedness

A namespace must be a non-empty string containing only letters, digits, hyphens, underscores, and dots. A key follows the same rules. A value must be valid JSON. These constraints are enforced on every Set call; malformed input is rejected before touching the in-memory store or disk.

On reload, the file is parsed and validated in full before the in-memory store is replaced. A malformed file leaves the existing store intact and logs a warning.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func TenantNamespace

func TenantNamespace(tenant string) string

TenantNamespace returns the conventional namespace string for a tenant.

Types

type DynConfig

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

DynConfig is the runtime configuration store.

func New

func New(filePath string) (*DynConfig, error)

New creates a DynConfig backed by filePath. If the file exists it is loaded immediately; if it does not exist the store starts empty. Returns an error only if the file exists but cannot be parsed.

func (*DynConfig) Delete

func (dc *DynConfig) Delete(namespace, key string) error

Delete removes namespace/key. If the namespace becomes empty it is removed too. Flushes to disk atomically. Returns nil if the key did not exist.

func (*DynConfig) Dump

func (dc *DynConfig) Dump() map[string]map[string]Value

Dump returns a deep copy of the entire store, suitable for serialisation.

func (*DynConfig) Get

func (dc *DynConfig) Get(namespace, key string) Value

Get returns the raw JSON value for namespace/key, or nil if not set.

func (*DynConfig) GetBool

func (dc *DynConfig) GetBool(namespace, key string) (bool, bool)

GetBool returns the bool value for namespace/key.

func (*DynConfig) GetFloat64

func (dc *DynConfig) GetFloat64(namespace, key string) (float64, bool)

GetFloat64 returns the float64 value for namespace/key.

func (*DynConfig) GetInt64

func (dc *DynConfig) GetInt64(namespace, key string) (int64, bool)

GetInt64 returns the int64 value for namespace/key. Returns (0, false) if the key is absent or not a JSON number.

func (*DynConfig) GetString

func (dc *DynConfig) GetString(namespace, key string) (string, bool)

GetString returns the string value for namespace/key.

func (*DynConfig) Namespace

func (dc *DynConfig) Namespace(namespace string) map[string]Value

Namespace returns a copy of all key/value pairs in a namespace. Returns nil if the namespace does not exist.

func (*DynConfig) Reload

func (dc *DynConfig) Reload() error

Reload re-reads the backing file. A malformed file leaves the existing in-memory store intact and returns the error. This is the public form used by the watcher and by tests.

func (*DynConfig) Set

func (dc *DynConfig) Set(namespace, key string, value Value) error

Set writes a value to namespace/key. The value must be valid JSON. The namespace and key must match [a-zA-Z0-9._-]+. The in-memory store is updated and the file is flushed atomically. Returns an error on any validation failure or I/O error; the in-memory store is not modified if the flush fails.

type Value

type Value = json.RawMessage

Value is a raw JSON value. It may be a number, string, boolean, null, array, or object — anything that is valid JSON.

type Watcher

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

Watcher periodically reloads the backing file of a DynConfig. It follows the same Start/Stop lifecycle as every other worker in the server. On each tick it calls dc.Reload(); a malformed file is logged as a warning and the existing in-memory store is preserved.

func NewWatcher

func NewWatcher(dc *DynConfig, interval time.Duration) *Watcher

NewWatcher creates a Watcher. Call Start to begin reloading.

func (*Watcher) Start

func (w *Watcher) Start()

Start launches the watcher goroutine.

func (*Watcher) Stop

func (w *Watcher) Stop()

Stop signals the watcher to stop and blocks until it has exited.

Jump to

Keyboard shortcuts

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