Documentation
¶
Overview ¶
Package corehttp serves the HTTP API of the relay proxy (the evaluation endpoints, the flag configuration, the flag change, the collection of events and the streams of changes) from a relaycore.Core. The handlers see the core through service.FlagSetResolver and service.FlagSet, which Adapter implements on the holder of the runtime and the evaluator.
Index ¶
- Constants
- Variables
- func MetricNames() []string
- func New(src Source, cfg *Config, opts ...api.Option) (api.Server, error)
- func ReadyStates() []string
- func Reasons() []string
- func SegmentMetricNames() []string
- func SettingsFrom(cfg *bootstrap.Config, version string) (api.Settings, error)
- func UnknownFieldsInfo(status *relaycore.Status) []model.UnknownFieldsInfo
- type Adapter
- func (a *Adapter) AdminKeyExists(apiKey string) bool
- func (*Adapter) AuthenticationEnabled() bool
- func (a *Adapter) FlagSet(apiKey string) (service.FlagSet, error)
- func (a *Adapter) FlagSetName(apiKey string) (string, error)
- func (*Adapter) IsDefaultFlagSet() bool
- func (a *Adapter) KeyExists(apiKey string) bool
- func (a *Adapter) Refresh(ctx context.Context) (service.RefreshOutcome, error)
- type ClientCounts
- type Config
- type ConfigCollector
- type DropCounts
- type Readiness
- type ReadinessOption
- type SegmentCollector
- type Source
- type StatusSource
- type Streams
- func (s *Streams) AwaitStarted(ctx context.Context) error
- func (s *Streams) Broadcast(change flagruntime.Change)
- func (s *Streams) Close(ctx context.Context) error
- func (s *Streams) Connected() ClientCounts
- func (s *Streams) Dropped() DropCounts
- func (s *Streams) Revalidate(current *flagruntime.Runtime)
- type StreamsConfig
Constants ¶
const ( // ReasonStarting: the bootstrap snapshot is not applied yet. ReasonStarting = "starting" // ReasonWaitingForManifest: the bootstrap snapshot is applied and no manifest was applied yet (nor a verified // copy in the state directory). ReasonWaitingForManifest = "waiting-for-manifest" // ReasonWaitingForSegmentStores: a generation is applied, and a segment store that it needs has no outcome of its // first dial yet (connected or failed). The wait ends at the latest at the dial bound of the connection. ReasonWaitingForSegmentStores = "waiting-for-segment-stores" // ReasonShuttingDown: the relay stops. ReasonShuttingDown = "shutting-down" )
The reasons of a 503 of /readyz and /startupz. The list is closed (Reasons).
const ( // ReadyEmpty: ready with zero flag sets. ReadyEmpty = "empty" // ReadyServing: every flag set and every object is fine. ReadyServing = "serving" // ReadyDegraded: a flag set or an object is not fine; the relay still serves. ReadyDegraded = "degraded" )
The states of the body of a 200 of /readyz. The list is closed (ReadyStates).
const DefaultMaxPending = 1024
DefaultMaxPending is the number of flag sets whose change can wait for a stream service before the oldest one is dropped.
Variables ¶
var ErrListeners = errors.New("corehttp: the listeners of the file are not supported")
ErrListeners is wrapped by the error of SettingsFrom when the listeners of the file are more than the server serves. The server fails closed: it does not start with a listener it would ignore.
var ErrNoStreams = errors.New("corehttp: the configuration has no streams")
ErrNoStreams is returned by New when the configuration has no streams: the server does not make services (they run goroutines) that nobody owns.
var ErrNotStarted = errors.New("corehttp: the stream workers did not start")
ErrNotStarted is returned by Streams.AwaitStarted when ctx ended before the workers started.
var ErrNotStopped = errors.New("corehttp: the stream workers did not stop")
ErrNotStopped is returned by Streams.Close when a stream service did not finish its last delivery before the context ended. The worker ends, and closes its service, as soon as that delivery returns.
Functions ¶
func New ¶
New builds the HTTP server of a core. The server serves the evaluation API, the flag configuration, the flag change, the collection of events and the streams from the runtime of the core. The caller starts it (Start) and stops it before the core.
func ReadyStates ¶
func ReadyStates() []string
ReadyStates are the closed states of a 200 of /readyz.
func SegmentMetricNames ¶
func SegmentMetricNames() []string
SegmentMetricNames lists the series of the dynamic segments that the relay exports besides the lookup series of segmentobs (the docs test checks that the pages only quote names that exist).
func SettingsFrom ¶
SettingsFrom returns the settings of the HTTP layer from a bootstrap file. The server serves one api listener and at most one ops listener, on its own address; a unix socket replaces the api listener. version is the version of the binary.
func UnknownFieldsInfo ¶
func UnknownFieldsInfo(status *relaycore.Status) []model.UnknownFieldsInfo
UnknownFieldsInfo summarises the unknown fields of the served documents by kind: the count and at most ten paths.
Types ¶
type Adapter ¶
type Adapter struct {
// contains filtered or unexported fields
}
Adapter implements the seam of the HTTP layer on a core: it finds the flag set of an API key in the current runtime and evaluates against it. It is safe for concurrent use and owns no goroutine.
func (*Adapter) AdminKeyExists ¶
AdminKeyExists reports whether the key is an admin key.
func (*Adapter) AuthenticationEnabled ¶
AuthenticationEnabled is true: a request needs a key, or else the generation has public flags.
func (*Adapter) FlagSetName ¶
FlagSetName returns the name of the flag set of the API key.
func (*Adapter) IsDefaultFlagSet ¶
IsDefaultFlagSet is false: a core always routes the keys to flag sets.
type ClientCounts ¶
type ClientCounts struct {
Websocket, SSE int
}
ClientCounts are the clients connected to each stream service.
type Config ¶
type Config struct {
// Settings are the settings of the HTTP layer.
Settings *api.Settings
// Logger is the logger of the server; nil discards the logs.
Logger *slog.Logger
// Streams serves the streams of flag changes, it is required. To get changes it must also be the Broadcast of
// the core. The caller owns it and closes it ([Streams.Close]) after the server stopped.
Streams *Streams
// Metrics are the metrics of the handlers; nil makes them. The server registers the collector of the config
// status ([NewConfigCollector]) in its registry.
Metrics *metric.Metrics
// Readiness is the readiness the probes answer from. The caller keeps it to call [Readiness.BeginShutdown] first
// on SIGTERM, before the listeners close; nil makes one that follows the core only ([relaycore.Status]).
Readiness *Readiness
}
Config is what the server needs besides the core.
type ConfigCollector ¶
type ConfigCollector struct {
// contains filtered or unexported fields
}
ConfigCollector exposes the status of a core: generations, reloads by outcome, staleness and the resources that are not ready. Series by resource name exist only for resources that are not ready (capped), the counts by state always exist, so the number of series follows the problems, not the flag sets, and never the flags: a scrape reads a summary that was made when the runtime was swapped, and evaluates no flag.
func NewConfigCollector ¶
func NewConfigCollector(src StatusSource) *ConfigCollector
NewConfigCollector returns the collector of the status of src.
func NewConfigCollectorSharing ¶
func NewConfigCollectorSharing(src StatusSource, ready *Readiness) *ConfigCollector
NewConfigCollectorSharing returns the collector of the status of src that shares the summaries of the readiness: a runtime is summarised once for the metrics and the probes, not once for each.
func (*ConfigCollector) Collect ¶
func (c *ConfigCollector) Collect(out chan<- prom.Metric)
Collect implements prom.Collector.
func (*ConfigCollector) Describe ¶
func (c *ConfigCollector) Describe(out chan<- *prom.Desc)
Describe implements prom.Collector.
type DropCounts ¶
type DropCounts struct {
Websocket, SSE uint64
}
DropCounts are the flag set changes that a stream service did not keep up with, by service.
type Readiness ¶
type Readiness struct {
// contains filtered or unexported fields
}
Readiness is the readiness state machine of a core. It follows the uptime rules: the relay is not ready until the bootstrap snapshot and a first manifest (or the verified copy of the state directory) are applied and every segment store that the served generation needs has had the outcome of its first dial (connected or failed, so a store that is down does not keep the relay out of rotation); from then on it stays ready whatever the control plane does, until Readiness.BeginShutdown or relaycore.Core.Shutdown. Degraded and stale content never makes it unready. It owns no goroutine and reads the status on demand.
func NewReadiness ¶
func NewReadiness(src StatusSource, opts ...ReadinessOption) *Readiness
NewReadiness returns the readiness of a core.
func (*Readiness) BeginShutdown ¶
func (r *Readiness) BeginShutdown()
BeginShutdown makes /readyz answer 503 shutting-down. Call it first on SIGTERM, before the listeners close.
type ReadinessOption ¶
type ReadinessOption func(*Readiness)
ReadinessOption changes how NewReadiness builds the readiness.
func WaitForSegmentStores ¶
func WaitForSegmentStores(wait bool) ReadinessOption
WaitForSegmentStores sets whether /readyz waits for the first dial outcome of the segment stores in use (the default is true: segments.waitForStores). False makes the relay ready as soon as a generation is applied.
type SegmentCollector ¶
type SegmentCollector struct {
// contains filtered or unexported fields
}
SegmentCollector exposes the dynamic segments: what the resolvers did, the state of each store and the served segments by health. A scrape reads the status; it calls no store and evaluates no flag.
func NewSegmentCollector ¶
func NewSegmentCollector(src StatusSource) *SegmentCollector
NewSegmentCollector returns the collector of the dynamic segments of src.
func (*SegmentCollector) Collect ¶
func (c *SegmentCollector) Collect(out chan<- prom.Metric)
Collect implements prom.Collector.
func (*SegmentCollector) Describe ¶
func (c *SegmentCollector) Describe(out chan<- *prom.Desc)
Describe implements prom.Collector.
type Source ¶
type Source interface {
// Holder returns the holder of the current runtime.
Holder() *flagruntime.Holder
// Evaluator returns the evaluator of the flag sets.
Evaluator() *flagruntime.Evaluator
// Status is the status of the core, for the probes, /info and the metrics.
Status() relaycore.Status
// Collect hands a feature event to the exporters.
Collect(event events.Event) bool
// DropNotPublic counts events refused for a flag that is not public.
DropNotPublic(count int)
// Refresh polls the control plane now.
Refresh(ctx context.Context) (relaycore.RefreshResult, error)
}
Source is what the adapter needs of a core.
type StatusSource ¶
StatusSource is what the readiness needs of a core: *relaycore.Core implements it.
type Streams ¶
type Streams struct {
// contains filtered or unexported fields
}
Streams are the SSE and websocket services of the streams of flag changes. A core calls Streams.Broadcast for every change (set it as the Broadcast of the dependencies of the core), and the server serves the connections. A change never waits for a client: each service has its own worker and queue, so a stalled websocket delays neither the core nor the SSE clients.
func NewStreams ¶
NewStreams returns the streams on the stream package services, built with its options. The workers carry the profiler labels of labels, so that what started them can find them (a leak test lists them by label).
func NewStreamsWith ¶
func NewStreamsWith(cfg *StreamsConfig) *Streams
NewStreamsWith returns the streams on the given services and starts the workers. Call Streams.Close to stop them.
func (*Streams) AwaitStarted ¶
AwaitStarted returns once the two workers entered their run loops, or when ctx ends first.
func (*Streams) Broadcast ¶
func (s *Streams) Broadcast(change flagruntime.Change)
Broadcast queues the change of a flag set for the clients connected to it and returns at once. A change that changes no flag is not queued. Changes of one flag set that wait together are merged into one (the SSE event only tells the client to fetch again, the websocket message carries the diff, so the diffs are merged to their net effect). When more flag sets wait than the bound, the oldest is dropped and counted.
func (*Streams) Close ¶
Close stops the workers and, once a worker is idle, closes its service. It returns when both ended or when ctx ends, whichever is first; a worker that is stuck in a delivery closes its service when the delivery returns.
func (*Streams) Connected ¶
func (s *Streams) Connected() ClientCounts
Connected is the number of clients connected to each stream service.
func (*Streams) Dropped ¶
func (s *Streams) Dropped() DropCounts
Dropped returns the number of flag set changes that were dropped because a service did not keep up.
func (*Streams) Revalidate ¶
func (s *Streams) Revalidate(current *flagruntime.Runtime)
Revalidate gives an open stream the rights of a new request. It closes the streams of the clients without a key when the runtime has no public flags anymore, and the streams of the clients with a key that does not route to the flag set of their stream anymore (the key was removed or rotated, the flag set was withdrawn or the key moved to another flag set). A key that is still valid is not touched, so a rotation with an overlap disconnects nobody. Set it as the AfterSwap hook of the core: it runs before the changes of the swap are queued, so a closed client gets none of them. It never waits for a client: the clients are out of the lists when it returns, and the sockets are closed by a worker of their own.
type StreamsConfig ¶
type StreamsConfig struct {
// Websocket and SSE are the services of the streams.
Websocket stream.WebsocketService
SSE stream.SSEService
// Labels is the context whose profiler labels the workers keep; nil means none.
Labels context.Context //nolint:containedctx // only its profiler labels are read
// Label is the value of the pprof label of the workers; it tells the goroutines of one Streams apart.
Label string
// MaxPending bounds the flag sets that wait for a service; 0 means [DefaultMaxPending].
MaxPending int
}
StreamsConfig configures NewStreamsWith.