Expand description
Weighted canary / A-B traffic splitting middleware.
CanaryLayer implements Middleware and distributes incoming requests
across a set of backends according to configurable weights. A backend with
weight 3 receives three times as many requests as one with weight 1.
Backends are contacted over plain HTTP/1.1, or over TLS when the backend
URL uses an https://, h2s://, or grpcs:// scheme (requires the
http-client or http2 feature — both pull in rustls). If a backend is
unavailable the next one in the rotation is tried; after exhausting all
backends the middleware returns 502 Bad Gateway.
§Example
use rust_web_server::app::App;
use rust_web_server::core::New;
use rust_web_server::canary::{CanaryLayer, WeightedBackend};
use rust_web_server::middleware::WithMiddleware;
// 75 % of traffic → stable, 25 % → canary
let app = WithMiddleware::new(App::new())
.wrap(
CanaryLayer::new(vec![
WeightedBackend::new("http://stable:8080", 3),
WeightedBackend::new("http://canary:8080", 1),
])
.path_prefix("/api"),
);Structs§
- Canary
Layer - Weighted traffic-splitting proxy middleware.
- Weighted
Backend - A backend URL together with a relative traffic weight.