Expand description
Tower middleware helpers: timeouts, concurrency limits, body limits, authentication, tenant scoping, and tracing spans.
Each helper returns a single tower layer (or middleware function) that
the server module composes into the axum router. Keeping the helpers thin
makes them easy to reuse in integration tests and benchmarks where a custom
stack is desired.
Structs§
- Auth
Config - Snapshot of authentication configuration loaded from the process environment at server start. Cloned cheaply into each request.
- Cors
Config - Cross-origin policy snapshot loaded from the process environment.
- Kernel
Publish Tokens - Allowlist of bearer tokens permitted to call
POST /kernels. - Tenant
Config - Snapshot of tenant-header policy loaded from the process environment.
- Trusted
Hosts - Parsed
Hostallowlist used byhost_validate.
Constants§
- DEFAULT_
CONCURRENCY_ LIMIT - Default process-wide cap on in-flight requests. Retained for callers that want a single number; production deployments should prefer the per-route caps below.
- DEFAULT_
REQUEST_ TIMEOUT - Default per-request timeout used by
crate::server::build_router. - ENV_
ALLOW_ DEV_ MODE - Environment variable that explicitly opts the gateway into dev mode
(auth disabled, every request passes through with
AuthContext::dev). - ENV_
API_ TOKENS - Environment variable carrying a comma-separated allowlist of bearer
tokens accepted by
bearer_auth. Empty / unset = dev mode pass-through (but only whenENV_ALLOW_DEV_MODEexplicitly opts in — see below). - ENV_
CORS_ ALLOWED_ ORIGINS - Environment variable carrying a comma-separated allowlist of origins permitted for cross-origin requests. Empty / unset = reject all cross-origin requests.
- ENV_
KERNEL_ PUBLISH_ TOKENS - Environment variable carrying a comma-separated allowlist of bearer
tokens that are additionally permitted to call
POST /kernels(the kernel-publish scope — seeKernelPublishTokens). - ENV_
REQUIRE_ TENANT - Environment variable that, when set to
1, makes theX-TensorWasm-Tenantheader mandatory. Otherwise its absence defaults to tenant0. - ENV_
TRUSTED_ HOSTS - Environment variable that, when set, restricts the set of
Hostheader values the server will accept. Comma-separated list of authority strings (e.g.api.example.com,api2.example.com:8443). Unset = accept anyHost, which is the previous behaviour but is unsafe behind a layered proxy that may pass arbitraryHostvalues through. - HEADER_
TENANT - Name of the header used to scope a request to a tenant.
- INVOKE_
CONCURRENCY_ LIMIT - Concurrent
/invokeceiling. Tighter than the default because invokes hold a Wasmtime instance lock acrosscall_async. - MAX_
AUTH_ HEADER_ BYTES - Maximum byte length permitted for an inbound
Authorizationheader value beforebearer_authwill even attempt to parse it. - MAX_
PATH_ LEN - Maximum byte length that a request path is allowed to occupy in a
tracing span attribute (see
sanitize_path). Anything longer is truncated with a…suffix to keep operator log lines bounded and to deny a hostile client a cheap way to balloon every log record that touches their request. - MAX_
REQUEST_ BODY_ BYTES - Maximum allowed inbound request body size, in bytes. 64 MiB.
- PROBE_
CONCURRENCY_ LIMIT - Per-route concurrency caps (api S-26). A single global semaphore lets a
probe storm starve
/invoke; per-route caps isolate the budgets. - READ_
CONCURRENCY_ LIMIT - Concurrent read-route ceiling (GETs that are not probes).
- WRITE_
CONCURRENCY_ LIMIT - Concurrent write-route ceiling (POST/PUT/DELETE excluding invoke).
Functions§
- bearer_
auth - Bearer-token authentication middleware.
- body_
limit_ layer - Build the global request-body size cap (64 MiB by default).
- concurrency_
limit_ layer - Build a process-wide concurrency limit layer that allows at most
maxin-flight requests. - cors_
layer - Build the CORS layer for the gateway router.
- extract_
tenant - Parse the
X-TensorWasm-Tenantheader into aTenantId, applying the configured policy. - host_
validate - Middleware: reject requests whose
Hostheader (or HTTP/2:authoritypseudo-header) is not in the configured allowlist. - normalize_
method - Normalise an HTTP method name for use as a tracing span attribute.
- sanitize_
path - Render a request path as a bounded, log-safe string for use as a tracing span attribute.
- tenant_
scope - Middleware that resolves the tenant from
X-TensorWasm-Tenantand stores it in the request’saxum::http::Extensionsfor handlers to pick up viaExtension<TenantId>. On parse failure / required-but-missing, emits the standard error envelope and short-circuits the chain. - timeout_
layer - Build a per-request timeout layer.
- trace_
layer - Build the default HTTP tracing layer.
- trace_
layer_ with_ propagation - Returns a
TraceLayerthat, in addition to the per-request span, reads the W3Ctraceparentheader from the incoming request and uses it as the parent context for the resulting span.