gpt4 book ai didi

asynchronous - 如何使用 futures "0.3"和 hyper "0.13.0-alpha.4"编写一个简单的 Rust 异步代理?

转载 作者:行者123 更新时间:2023-11-29 07:55:07 25 4
gpt4 key购买 nike

我正在尝试重写代理 exampleAsynchronous Programming in Rust通过迁移到预订:

futures-preview = { version = "0.3.0-alpha.19", features = ["async-await"]}`
hyper = "0.13.0-alpha.4"`

来自:

futures-preview = { version = "=0.3.0-alpha.17", features = ["compat"] }`
hyper = "0.12.9"

当前示例转换返回的 Future来自futures 0.3进入 futures 0.1 ,因为 hyper = "0.12.9"futures 0.3 不兼容的异步/等待。

我的代码:

use {
futures::future::{FutureExt, TryFutureExt},
hyper::{
rt::run,
service::{make_service_fn, service_fn},
Body, Client, Error, Request, Response, Server, Uri,
},
std::net::SocketAddr,
std::str::FromStr,
};

fn forward_uri<B>(forward_url: &'static str, req: &Request<B>) -> Uri {
let forward_uri = match req.uri().query() {
Some(query) => format!("{}{}?{}", forward_url, req.uri().path(), query),
None => format!("{}{}", forward_url, req.uri().path()),
};

Uri::from_str(forward_uri.as_str()).unwrap()
}

async fn call(
forward_url: &'static str,
mut _req: Request<Body>,
) -> Result<Response<Body>, hyper::Error> {
*_req.uri_mut() = forward_uri(forward_url, &_req);
let url_str = forward_uri(forward_url, &_req);
let res = Client::new().get(url_str).await;
res
}

async fn run_server(forward_url: &'static str, addr: SocketAddr) {
let forwarded_url = forward_url;
let serve_future = service_fn(move |req| call(forwarded_url, req).boxed());

let server = Server::bind(&addr).serve(serve_future);
if let Err(err) = server.await {
eprintln!("server error: {}", err);
}
}

fn main() {
// Set the address to run our socket on.
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
let url = "http://127.0.0.1:9061";
let futures_03_future = run_server(url, addr);
run(futures_03_future);
}

首先,我收到 server 的错误在run_server功能:

the trait tower_service::Service<&'a
hyper::server::tcp::addr_stream::AddrStream>
is not implemented for hyper::service::service::ServiceFn<[closure@src/main.rs:35:35: 35:78
forwarded_url:_], hyper::body::body::Body>

此外,我不能使用 hyper::rt::run因为它可能在 hyper = 0.13.0-alpha.4 中以不同的方式实现.

如果您能告诉我您的修复方法,我将不胜感激。

最佳答案

通过这个issue , 为每个需要创建的连接创建一个新服务 MakeServicehyper = "0.13.0-alpha.4" 中。您可以使用 make_service_fn 创建带有闭包的 MakeService

Also, I cannot use hyper::rt::run because it might have been implemented differently in hyper = 0.13.0-alpha.4.

正确,hyper::rt::run 正在调用 tokio::run,它已从 api 中删除,但目前我不知道原因。您可以通过自己调用 tokio::run 或使用 #[tokio::main] 注释来运行您的 future 。为此,您需要将 tokio 添加到您的 cargo 中:

#this is the version of tokio inside hyper "0.13.0-alpha.4"
tokio = "=0.2.0-alpha.6"

然后像这样更改您的 run_server:

async fn run_server(forward_url: &'static str, addr: SocketAddr) {
let server = Server::bind(&addr).serve(make_service_fn(move |_| {
async move { Ok::<_, Error>(service_fn(move |req| call(forward_url, req))) }
}));
if let Err(err) = server.await {
eprintln!("server error: {}", err);
}
}

主要:

#[tokio::main]
async fn main() -> () {
// Set the address to run our socket on.
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
let url = "http://www.google.com:80"; // i have tested with google

run_server(url, addr).await
}

关于asynchronous - 如何使用 futures "0.3"和 hyper "0.13.0-alpha.4"编写一个简单的 Rust 异步代理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58505000/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com