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.
**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.
**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).
**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 ¶
const Day = 24 * time.Hour
Day is the unit section 15.1 writes horizons and TTLs in.
Variables ¶
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 ¶
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 ¶
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 ¶
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.
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.
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) Replacement ¶
Replacement reports the replacement for a retired key.
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.
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 ¶
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 ¶
All returns every effective value, sorted by key. This is the body of `GET /v1/admin/settings`.
func (*Settings) Patch ¶
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.
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 ¶
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 (Value) JSON ¶
JSON is MarshalJSON without the error, for the many places that hold a value already known to be well-typed.
func (Value) MarshalJSON ¶
MarshalJSON writes the wire form: a number for an int, a bool for a bool, a string for a duration or an enum.