Skip to main content

Module reality

Module reality 

Source
Expand description

Reality-slider mock/proxy switching middleware (#222). Moved from mockforge_http::reality_proxy under #555 phase 8. Only foreign dep is mockforge_core::consistency::UnifiedState, already in the proxy crate’s dep graph. mockforge_http::reality_proxy is now a thin shim re-exporting from here. Reality-slider-driven mock/proxy switching middleware (#222).

When MOCKFORGE_PROXY_UPSTREAM is set on the process, this middleware probabilistically forwards a fraction of incoming requests to that URL based on the active workspace’s reality_continuum_ratio. The fraction is per-request: ratio 0.0 = always-mock, 1.0 = always-proxy, 0.5 = coin-flip per request.

§Design

The middleware is a no-op when:

  • MOCKFORGE_PROXY_UPSTREAM is unset (e.g., local dev)
  • the request has no associated UnifiedState extension (set by the consistency middleware upstream of this one)
  • the resolved ratio is exactly 0.0

When proxying, the middleware reconstructs the request against the upstream base URL preserving method, path, query, headers, and body, then streams the upstream response back to the caller. Any failure falls through to the mock chain — the mock is the durable path; the upstream is best-effort.

Wiring: insert the layer between consistency_middleware (which injects UnifiedState) and the route handlers. The dependency on UnifiedState is read-only, so ordering relative to recording or tracing layers doesn’t matter.

Moved from mockforge_http::reality_proxy under #555 phase 8 — the file’s only foreign dep (mockforge_core::consistency::UnifiedState) already lived in this crate’s dep graph via mockforge-core. mockforge_http::reality_proxy is now a thin shim re-exporting from here.

Structs§

RealityProxyConfig
Cheap-to-clone handle holding the upstream base URL and a shared reqwest client. Constructed once at server startup; the layer closure holds an Arc<RealityProxyConfig> so per-request work is just an arc-clone.

Functions§

reality_proxy_middleware
The middleware function. Reads reality_continuum_ratio from the UnifiedState request extension, rolls a per-request RNG, and either forwards to upstream or hands off to the next layer (mock chain).