corehttp

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2026 License: MIT Imports: 39 Imported by: ? 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 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

View Source
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).

View Source
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).

View Source
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

View Source
var ErrEmptyPEM = errors.New("corehttp: the secret is empty")

ErrEmptyPEM is wrapped by the error of SettingsWithSecrets when tls.cert or tls.key resolves to nothing, or to white space only: the listener would otherwise fall back to a generated certificate.

View Source
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.

View Source
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.

View Source
var ErrNotStarted = errors.New("corehttp: the stream workers did not start")

ErrNotStarted is returned by Streams.AwaitStarted when ctx ended before the workers started.

View Source
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 MetricNames

func MetricNames() []string

MetricNames are the metrics this collector exports.

func New

func New(src Source, cfg *Config, opts ...api.Option) (api.Server, error)

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 Reasons

func Reasons() []string

Reasons are the closed reasons of a 503.

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

func SettingsFrom(cfg *bootstrap.Config, version string) (api.Settings, error)

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. A listener whose certificate and key are secrets needs SettingsWithSecrets.

func SettingsWithSecrets added in v0.3.0

func SettingsWithSecrets(cfg *bootstrap.Config, version string, secrets bootstrap.LoadOptions) (api.Settings, error)

SettingsWithSecrets is SettingsFrom for a file whose listeners get their certificate and key as secrets: secrets reads them.

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 NewAdapter

func NewAdapter(src Source) *Adapter

NewAdapter returns the adapter of a core.

func (*Adapter) AdminKeyExists

func (a *Adapter) AdminKeyExists(apiKey string) bool

AdminKeyExists reports whether the key is an admin key.

func (*Adapter) AuthenticationEnabled

func (*Adapter) AuthenticationEnabled() bool

AuthenticationEnabled is true: a request needs a key, or else the generation has public flags.

func (*Adapter) FlagSet

func (a *Adapter) FlagSet(apiKey string) (service.FlagSet, error)

FlagSet returns the flag set of the API key.

func (*Adapter) FlagSetName

func (a *Adapter) FlagSetName(apiKey string) (string, error)

FlagSetName returns the name of the flag set of the API key.

func (*Adapter) IsDefaultFlagSet

func (*Adapter) IsDefaultFlagSet() bool

IsDefaultFlagSet is false: a core always routes the keys to flag sets.

func (*Adapter) KeyExists

func (a *Adapter) KeyExists(apiKey string) bool

KeyExists reports whether the key is a flag set key or an admin key.

func (*Adapter) Refresh

func (a *Adapter) Refresh(ctx context.Context) (service.RefreshOutcome, error)

Refresh polls the control plane now, through the poll of the schedule.

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.

func (*Readiness) Ready

func (r *Readiness) Ready() (int, model.ReadyResponse)

Ready answers /readyz.

func (*Readiness) Started

func (r *Readiness) Started() (int, model.ReadyResponse)

Started answers /startupz: 200 once the bootstrap snapshot is applied, and from then on.

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

type StatusSource interface {
	Status() relaycore.Status
}

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

func NewStreams(labels context.Context, opts ...stream.Option) *Streams

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

func (s *Streams) AwaitStarted(ctx context.Context) error

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

func (s *Streams) Close(ctx context.Context) error

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.

Jump to

Keyboard shortcuts

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