Skip to main content

Module reality_proxy

Module reality_proxy 

Source
Expand description

Reality-slider-driven mock/proxy switching middleware (#222) 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.

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