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 ¶
- func TenantNamespace(tenant string) string
- type DynConfig
- func (dc *DynConfig) Delete(namespace, key string) error
- func (dc *DynConfig) Dump() map[string]map[string]Value
- func (dc *DynConfig) Get(namespace, key string) Value
- func (dc *DynConfig) GetBool(namespace, key string) (bool, bool)
- func (dc *DynConfig) GetFloat64(namespace, key string) (float64, bool)
- func (dc *DynConfig) GetInt64(namespace, key string) (int64, bool)
- func (dc *DynConfig) GetString(namespace, key string) (string, bool)
- func (dc *DynConfig) Namespace(namespace string) map[string]Value
- func (dc *DynConfig) Reload() error
- func (dc *DynConfig) Set(namespace, key string, value Value) error
- type Value
- type Watcher
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func TenantNamespace ¶
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 ¶
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 ¶
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) GetFloat64 ¶
GetFloat64 returns the float64 value for namespace/key.
func (*DynConfig) GetInt64 ¶
GetInt64 returns the int64 value for namespace/key. Returns (0, false) if the key is absent or not a JSON number.
func (*DynConfig) Namespace ¶
Namespace returns a copy of all key/value pairs in a namespace. Returns nil if the namespace does not exist.
func (*DynConfig) Reload ¶
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 ¶
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 ¶
NewWatcher creates a Watcher. Call Start to begin reloading.