moonzero · control plane v0.15.0 · 49 packages · 4 targets · Apache-2.0 docs / control / engine GitHub

02 / CONTROL

engine

RestEngine reads a RestConf and assembles one ASGI application: it builds the clock, the metrics, the connection budget, the breaker, the shaper, the tracer and the span recorder the middlewares run on, installs the layers the config asked for in a fixed order, and keeps the spans so a test can read a trace without a collector.

struct Layer

structOne layer of the assembled chain: name for the handler, and the middleware that stands in for it.

struct RestEngine

structThe engine that turns a RestConf into a runnable service. It owns the state the built-in layers need — the connection permits, the breaker window, the metric set, the two shedders — so every request shares one of each, and it installs exactly the layers Middlewares asks for.

fn RestEngine::new( conf : @conf.RestConf, clock~ : @clock.Clock = @clock.Clock::system(), logger? : @logx.Logger, metrics? : @handler.ServerMetrics, usage? : () -> Int64, tracer? : @trace.Tracer, signature? : @handler.SignatureVerifier) -> RestEngine raise @conf.Refused

fnBuild the engine for conf, pointing the logger at the configured level. usage is the CPU meter the shedder reads, per mille; tracer decides where spans go (the default keeps them in an in-process recorder the engine owns); signature is the verifier the content-security layer checks against. Raises Refused for a strict signature config with no keys, and for a metric registry that will not register its families.

fn RestEngine::conf(self : RestEngine) -> @conf.RestConf

fnThe config the engine was built from.

fn RestEngine::metrics(self : RestEngine) -> @handler.ServerMetrics

fnThe metric set the prometheus layer records into — the same one mount_metrics publishes.

fn RestEngine::shedder(self : RestEngine) -> @shedder.Shedder

fnThe shedder ordinary routes draw on.

fn RestEngine::priority_shedder(self : RestEngine) -> @shedder.Shedder

fnThe shedder a route opts into with RouteOptions::priority: it sheds at half the distance to saturation, so a busy service sacrifices ordinary traffic before these calls.

fn RestEngine::tracer(self : RestEngine) -> @trace.Tracer

fnThe tracer the trace layer runs on.

fn RestEngine::spans(self : RestEngine) -> Array[@trace.SpanData]

fnThe spans the engine's default tracer has finished, oldest first. A read-out for tests and for a service that ships spans somewhere of its own.

fn RestEngine::layers(self : RestEngine) -> Array[Layer]

fnThe chain the flags ask for, outermost first, in the order the reference implementation builds it: trace, log, prometheus, max conns, breaker, shedding, timeout, recover, max bytes, gunzip — with the content-security layer ahead of all of them when a verifier is configured, because it must see the body before anything else reads it. A layer whose configured value disables it is left out even when its flag is on: no shedder without a CpuThreshold, no timeout without a budget, no body cap without a MaxBytes. Metrics flag is its internal stat.Metrics sink, which records the same numbers the Prometheus families do here; it loads and installs nothing.

fn RestEngine::names(self : RestEngine) -> Array[String]

fnThe names of the layers layers would install, outermost first.

fn RestEngine::build(self : RestEngine, app : @moonapi.App) -> Server

fnAssemble app under the configured chain. The outermost layer is named first while Server::use_ makes the most recent layer outermost, so the list goes on back to front.