Negotiate a pool of services
The negotiate pool allows for a service that can decide between two service
types based on an intermediate return value. It differs from typical
routing since it doesn't depend on the request, but the response.
The original use case is support ALPN upgrades to HTTP/2, with a fallback
to HTTP/1.
Example
# async fn run() -> Result<(), Box<dyn std::error::Error>> {
# struct Conn;
# impl Conn { fn negotiated_protocol(&self) -> &[u8] { b"h2" } }
# let some_tls_connector = tower::service::service_fn(|_| async move {
# Ok::<_, std::convert::Infallible>(Conn)
# });
# let http1_layer = tower::layer::layer_fn(|s| s);
# let http2_layer = tower::layer::layer_fn(|s| s);
let mut pool = hyper_util::client::pool::negotiate::builder()
.connect(some_tls_connector)
.inspect(|c| c.negotiated_protocol() == b"h2")
.fallback(http1_layer)
.upgrade(http2_layer)
.build();
let mut svc = pool.call(http::Uri::from_static("https://hyper.rs")).await?;
svc.ready().await;
# let some_http_req = http::Request::new(());
let resp = svc.call(some_http_req).await?;
# Ok(())
# }