我正在尝试使用hyper
编写一个服务器,它将传入连接的远程(客户机)地址向下传递到Layer
堆栈(我使用ServiceBuilder
构建的堆栈)。
我已经 * 尝试 * 使用例子from the hyper
docs和来自Rust论坛的this example;然而,这两者都
- 将数据向下传递给一个 * 单个 * 处理函数,而不是一个服务层堆栈
- 我有一个
Result<Response, Infallible>
的返回类型,这是我 * 不 * 想要的(我希望能够在不返回响应的情况下断开连接)。
下面是我的一个尝试(我已经尝试了几种方法):
use std::{
net::SocketAddr,
time::Duration,
};
use hyper::{
Body, Request, Response, Server,
server::conn::AddrStream,
service::{
make_service_fn,
service_fn,
},
};
use tower::{
Service, ServiceBuilder,
timeout::TimeoutLayer,
};
async fn dummy_handle(req: Request<Body>) -> Result<Response<Body>, hyper::Error> {
let response_text = format!(
"{:?} {} {}", req.version(), req.method(), req.uri()
);
let response = Response::new(Body::from(response_text));
Ok(response)
}
# [tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let addr = SocketAddr::from(([127, 0, 0, 1], 8080));
// Dummy stack of service layers I want to wrap.
let service = ServiceBuilder::new()
.layer(TimeoutLayer::new(Duration::from_millis(1000 * 60)))
.service_fn(dummy_handle);
let make_svc = make_service_fn(|socket: &AddrStream| {
let remote_addr = socket.remote_addr();
let mut inner_svc = service.clone();
let outer_svc = service_fn(move |mut req: Request<Body>| async {
req.extensions_mut().insert(remote_addr);
inner_svc.call(req)
});
async move { outer_svc }
});
Server::bind(&addr)
.serve(make_svc)
.await?;
Ok(())
}
我完全理解在这里包含错误消息是有帮助的;然而,这是Rust编译器吐出 pages(或者至少是screenfully)的晦涩东西的情况之一,所以我将把自己限制在几个选择的例子中。
首先,我经常听到这样的话:
type mismatch resolving `<impl Future<Output = [async output]> as Future>::Output == Result<_, _>`
例如,在此之前:
39 | let outer_svc = service_fn(move |mut req: Request<Body>| async {
| _____________________________________-___________________________________-
| | ____________________________________|
| ||
40 | || req.extensions_mut().insert(remote_addr);
41 | || inner_svc.call(req)
42 | || });
| || -
| ||_________|
| |__________the expected closure
| the expected `async` block
...
48 | .serve(make_svc)
| ----- ^^^^^^^^ expected struct `service::util::ServiceFn`, found enum `Result`
| |
| required by a bound introduced by this call
接下来的错误消息似乎完全矛盾:
[ several lines identical to above elided here ]
48 | .serve(make_svc)
| ^^^^^ expected enum `Result`, found struct `service::util::ServiceFn`
我只是不知道编译器想要我做什么。
2条答案
按热度按时间72qzrwbm1#
试试看:
您正在返回一个
future
,该future
返回另一个future
:这是一个
Future<Output = Future<...>>
。因此,你需要把你的结束语变成这样:
mepcadol2#
如果您不需要
ServiceBuilder
,则可以执行以下操作:其他