settings

package
v1.4.2 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2026 License: AGPL-3.0 Imports: 9 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package settings is the runtime settings table of spec section 15.1: the keys that are mutable through `PATCH /v1/admin/settings` without an environment change and without a redeploy.

Three things about this package are load-bearing.

  1. **The registry is declarative.** One `Def` per key carries the default, the bounds, the scope, the type, mutability and the restart requirement. A key that is not declared does not exist; there is no path that reads an undeclared key and no path that writes one.

  2. **Scope is not decoration.** `ScopePerAccount` says the value is applied to each account independently, so a two-account deployment spends twice the budget. Recording it wrongly is how a deployment does twice the work it was configured for, or half (section 15.1).

  3. **Persistence is an interface.** This package never imports internal/store; the store implements `Store` against the `settings` table of section 4.2 and this package resolves precedence over it.

Index

Constants

View Source
const Day = 24 * time.Hour

Day is the unit section 15.1 writes horizons and TTLs in.

Variables

View Source
var Retired = map[string]string{}

Retired is the retired-key table of section 15.1. The value is the replacement key, or "" for a key with no replacement.

It is empty today because nothing has been retired yet. The mechanism exists anyway, and is tested: a retired key must become an *unknown* key that answers with its replacement, never a key that is silently written and then read by nothing.

Functions

func ParseDuration

func ParseDuration(s string) (time.Duration, error)

ParseDuration is time.ParseDuration extended with a `d` (day) unit, because section 15.1 writes horizons and TTLs in days and `8760h` is not a value an operator should have to compute.

Types

type Change

type Change struct {
	Key  string
	From Value
	To   Value
	// RequiresRestart repeats the declaration, so the response can tell an
	// operator which of the keys they just changed is not yet in force.
	RequiresRestart bool
}

Change is one accepted key in a PATCH.

type Def

type Def struct {
	// Key is the wire name, as it appears in `PATCH /v1/admin/settings`.
	Key string
	// Kind is the type.
	Kind Kind
	// Default is the value with no environment and no database row.
	Default Value
	// Min and Max are inclusive bounds. Both nil means unbounded.
	Min *Value
	Max *Value
	// EnumValues constrains a KindEnum setting.
	EnumValues []string
	// Scope is section 15.1's scope column.
	Scope Scope
	// Mutable says whether `PATCH /v1/admin/settings` may write the key at
	// all.
	Mutable bool
	// DownwardOnly says a new value may only be less than or equal to the
	// current effective one. `media.upload_max_bytes` is the one key with
	// this property: raising the ceiling at runtime would let a caller
	// re-open a limit an operator closed.
	DownwardOnly bool
	// RequiresRestart is honest rather than aspirational. Section 15.1
	// singles out `logging.level` as reloadable without restart; every other
	// key says a restart is required rather than claim a hot reload nothing
	// implements.
	RequiresRestart bool
	// Env is the environment variable that overrides the default, or "" if
	// the key has none. Only declared names are ever read, so no key gains
	// an environment override by accident.
	Env string
	// Note is the section 15.1 remark, carried so the admin surface can
	// explain the multiplication an operator is signing up for.
	Note string
}

Def is the declaration of one runtime setting.

func (Def) BoundsString

func (d Def) BoundsString() string

BoundsString renders the bounds the way an error message needs them.

type Effective

type Effective struct {
	Key             string `json:"key"`
	Value           Value  `json:"value"`
	Source          Source `json:"source"`
	Default         Value  `json:"default"`
	Bounds          string `json:"bounds"`
	Scope           Scope  `json:"scope"`
	Type            Kind   `json:"type"`
	Mutable         bool   `json:"mutable"`
	RequiresRestart bool   `json:"requires_restart"`
	Note            string `json:"note,omitempty"`
}

Effective is one row of `GET /v1/admin/settings`: the value in force, where it came from, and what it would take to change it.

type Error

type Error struct {
	Key string
	Msg string
}

Error is a settings refusal. Every one of them names the key, so an operator reading a rejected PATCH knows which of the keys they sent was the problem.

func AsError

func AsError(err error) (*Error, bool)

AsError returns the *Error a settings failure carries, if it is one.

func (*Error) Code

func (e *Error) Code() string

Code is the section 7.2 error code every settings refusal maps to.

func (*Error) Error

func (e *Error) Error() string

type Kind

type Kind string

Kind is a setting's type. It decides how a value is parsed from JSON, how it is compared against its bounds, and how it is rendered back out.

const (
	// KindInt is a whole number: counts, page sizes, byte budgets.
	KindInt Kind = "int"
	// KindDuration is a duration, carried in JSON as a string in Go's
	// duration syntax extended with `d` for days ("15m", "365d").
	KindDuration Kind = "duration"
	// KindBool is a boolean.
	KindBool Kind = "bool"
	// KindEnum is a string constrained to a declared set.
	KindEnum Kind = "enum"
)

type Memory

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

Memory is an in-memory Store for tests.

func NewMemory

func NewMemory() *Memory

NewMemory builds an empty in-memory store.

func (*Memory) All

func (s *Memory) All(_ context.Context) (map[string]string, error)

func (*Memory) Get

func (s *Memory) Get(_ context.Context, key string) (string, bool, error)

func (*Memory) Put

func (s *Memory) Put(_ context.Context, key, valueJSON string) error

type Registry

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

Registry is the set of declared settings plus the retired-key table.

func NewRegistry

func NewRegistry() *Registry

NewRegistry builds the registry of section 15.1's runtime table plus the OAuth and admin TTLs of section 9.6.

The defaults, bounds and scopes here are the specification's, transcribed once. A table test in this package asserts them against the same literals read out of the spec, in both directions, so neither a new key nor a changed bound can drift without a named failure.

func (*Registry) Def

func (r *Registry) Def(key string) (Def, bool)

Def returns the declaration for a key.

func (*Registry) Keys

func (r *Registry) Keys() []string

Keys returns every declared key, sorted.

func (*Registry) Replacement

func (r *Registry) Replacement(key string) (string, bool)

Replacement reports the replacement for a retired key.

func (*Registry) Retire

func (r *Registry) Retire(key, replacement string)

Retire records a retired key and its replacement. A retired key is an unknown key: it can never be written again, and the refusal names the replacement.

type Scope

type Scope string

Scope says whether a value is one server-wide budget or a server-wide value applied to each account independently (spec section 15.1).

This is the field most worth getting right. `ScopePerAccount` on `backfill.concurrency` means the worst case is that value multiplied by `accounts.max_concurrent`; reading it as server-wide is how a two-account deployment does half the work it was configured for, and reading a server-wide key as per-account is how it does twice.

const (
	// ScopeServer is one budget for the whole server, however many accounts
	// it holds.
	ScopeServer Scope = "server"
	// ScopePerAccount is a server-wide value applied to each account
	// independently: the cost multiplies by account count.
	ScopePerAccount Scope = "per account"
)

type Settings

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

Settings resolves effective values over a Store and validates writes.

Precedence is database, then environment, then default: a value an operator set through the admin API outranks the environment it was started with, which outranks the declaration.

func New

func New(reg *Registry, store Store, env func(string) string) *Settings

New builds a Settings over a registry, a store, and an environment lookup. env may be nil, in which case no key has an environment source.

func (*Settings) All

func (s *Settings) All(ctx context.Context) ([]Effective, error)

All returns every effective value, sorted by key. This is the body of `GET /v1/admin/settings`.

func (*Settings) Get

func (s *Settings) Get(ctx context.Context, key string) (Effective, error)

Get returns one effective value.

func (*Settings) Patch

func (s *Settings) Patch(ctx context.Context, body map[string]json.RawMessage) ([]Change, error)

Patch validates the whole body and then writes it.

Section 7.7: any invalid key rejects the request and changes nothing. Validation therefore runs over every key first and returns on the first refusal, before a single Put; a body of ten good keys and one bad one leaves the database exactly as it was.

func (*Settings) Registry

func (s *Settings) Registry() *Registry

Registry returns the registry this Settings resolves against.

type Source

type Source string

Source is where an effective value came from (section 15.1).

const (
	// SourceDefault is the value declared in the registry.
	SourceDefault Source = "default"
	// SourceEnvironment is an environment variable.
	SourceEnvironment Source = "environment"
	// SourceDatabase is a row in the `settings` table.
	SourceDatabase Source = "database"
)

type Store

type Store interface {
	// Get returns the stored JSON for a key. ok is false when no row exists.
	Get(ctx context.Context, key string) (valueJSON string, ok bool, err error)
	// Put writes the stored JSON for a key, inserting or replacing.
	Put(ctx context.Context, key, valueJSON string) error
	// All returns every stored row, keyed by setting key.
	All(ctx context.Context) (map[string]string, error)
}

Store is the persistence this package needs and nothing more. The `settings` table of section 4.2 is `(key, value_json, updated_at_ms)`; the store implements this interface against it, and this package never imports internal/store.

type Value

type Value struct {
	Kind Kind
	Int  int64
	Dur  time.Duration
	Bool bool
	Str  string
}

Value is one typed setting value. It is a struct rather than an `any` so that a bounds comparison cannot silently compare an int to a duration.

func Bool

func Bool(b bool) Value

Bool builds a boolean value.

func Duration

func Duration(d time.Duration) Value

Duration builds a duration value.

func Enum

func Enum(s string) Value

Enum builds an enumerated string value.

func Int64

func Int64(v int64) Value

Int64 builds an integer value.

func (Value) Equal

func (v Value) Equal(o Value) bool

Equal reports whether two values are the same type and the same value.

func (Value) JSON

func (v Value) JSON() string

JSON is MarshalJSON without the error, for the many places that hold a value already known to be well-typed.

func (Value) MarshalJSON

func (v Value) MarshalJSON() ([]byte, error)

MarshalJSON writes the wire form: a number for an int, a bool for a bool, a string for a duration or an enum.

func (Value) String

func (v Value) String() string

String renders a value the way section 15.1 writes it.

Jump to

Keyboard shortcuts

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