gpt4 book ai didi

rust - 我可以在 Rocket 中重复使用 Tokio Core 和 Hyper Client 吗?

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

在收到用户的传入请求后,我想使用 Hyper 0.11 发出客户端请求,我使用火箭处理并使用尽可能少的资源。如果我不能重用 CoreClient,我将不得不在每次 Rocket 发出请求时创建它们。每次服务器响应请求时,这将在高性能环境中占用太多资源来创建 CoreClient。所以我使用 State 使对象可用于所有“点火”。在 Hyper 0.10.13 中它可以工作,但在带有 Tokio 的 Hyper 0.11 中我收到大量错误消息。

cargo .toml:

rocket = "0.3.3"
rocket_codegen = "0.3.3"
hyper = "0.11"
futures = "0.1"
tokio-core = "0.1"

主要.rs

#![feature(plugin, decl_macro)]
#![plugin(rocket_codegen)]
extern crate futures;
extern crate hyper;
extern crate rocket;
extern crate tokio_core;
use rocket::State;
use hyper::*;
use hyper::header::{ContentLength, ContentType};
use futures::{Future, Stream};
use tokio_core::reactor::Core;

fn main() {
let mut core = Core::new().unwrap();
let client = Client::new(&core.handle());
rocket::ignite()
.mount("/", routes![index])
.manage(client.clone())
.launch();
}

#[post("/", data = "<var>")]
fn index(var: String, client: State<hyper::Client<tokio_core::reactor::Handle>>) -> &'static str {
let json = r#"{"library":"hyper"}"#;
let uri = "http://xxx.xxx.xxx.xxx".parse().unwrap();
let mut req = Request::new(Method::Post, uri);
req.headers_mut().set(ContentType::json());
req.headers_mut().set(ContentLength(json.len() as u64));
req.set_body(json);
let post = client.request(req).and_then(|res| {
//println!("POST: {}", res.status());
res.body().concat2()
});
}

我收到如下错误消息:

error[E0277]: the trait bound `std::rc::Weak<std::cell::RefCell<tokio_core::reactor::Inner>>: std::marker::Send` is not satisfied in `hyper::Client<tokio_core::reactor::Handle>`
--> src/main.rs:23:1
|
23 | / fn index(var: String, client: State<hyper::Client<tokio_core::reactor::Handle>>) -> &'static str {
24 | | let json = r#"{"library":"hyper"}"#;
25 | | let uri = "http://xxx.xxx.xxx.xxx".parse().unwrap();
26 | | let mut req = Request::new(Method::Post, uri);
... |
33 | | });
34 | | }
| |_^ `std::rc::Weak<std::cell::RefCell<tokio_core::reactor::Inner>>` cannot be sent between threads safely
|
= help: within `hyper::Client<tokio_core::reactor::Handle>`, the trait `std::marker::Send` is not implemented for `std::rc::Weak<std::cell::RefCell<tokio_core::reactor::Inner>>`
= note: required because it appears within the type `tokio_core::reactor::Handle`
= note: required because it appears within the type `hyper::Client<tokio_core::reactor::Handle>`
= note: required by `rocket::State`

error[E0277]: the trait bound `std::rc::Weak<std::cell::RefCell<tokio_core::reactor::Inner>>: std::marker::Sync` is not satisfied in `hyper::Client<tokio_core::reactor::Handle>`
--> src/main.rs:23:1
|
23 | / fn index(var: String, client: State<hyper::Client<tokio_core::reactor::Handle>>) -> &'static str {
24 | | let json = r#"{"library":"hyper"}"#;
25 | | let uri = "http://xxx.xxx.xxx.xxx".parse().unwrap();
26 | | let mut req = Request::new(Method::Post, uri);
... |
33 | | });
34 | | }
| |_^ `std::rc::Weak<std::cell::RefCell<tokio_core::reactor::Inner>>` cannot be shared between threads safely
|
= help: within `hyper::Client<tokio_core::reactor::Handle>`, the trait `std::marker::Sync` is not implemented for `std::rc::Weak<std::cell::RefCell<tokio_core::reactor::Inner>>`
= note: required because it appears within the type `tokio_core::reactor::Handle`
= note: required because it appears within the type `hyper::Client<tokio_core::reactor::Handle>`
= note: required by `rocket::State`

error[E0277]: the trait bound `std::rc::Rc<std::cell::RefCell<hyper::client::pool::PoolInner<tokio_proto::util::client_proxy::ClientProxy<tokio_proto::streaming::message::Message<hyper::proto::MessageHead<hyper::proto::RequestLine>, hyper::Body>, tokio_proto::streaming::message::Message<hyper::proto::MessageHead<hyper::proto::RawStatus>, tokio_proto::streaming::body::Body<hyper::Chunk, hyper::Error>>, hyper::Error>>>>: std::marker::Send` is not satisfied in `hyper::Client<tokio_core::reactor::Handle>`
--> src/main.rs:23:1
|
23 | / fn index(var: String, client: State<hyper::Client<tokio_core::reactor::Handle>>) -> &'static str {
24 | | let json = r#"{"library":"hyper"}"#;
25 | | let uri = "http://xxx.xxx.xxx.xxx".parse().unwrap();
26 | | let mut req = Request::new(Method::Post, uri);
... |
33 | | });
34 | | }
| |_^ `std::rc::Rc<std::cell::RefCell<hyper::client::pool::PoolInner<tokio_proto::util::client_proxy::ClientProxy<tokio_proto::streaming::message::Message<hyper::proto::MessageHead<hyper::proto::RequestLine>, hyper::Body>, tokio_proto::streaming::message::Message<hyper::proto::MessageHead<hyper::proto::RawStatus>, tokio_proto::streaming::body::Body<hyper::Chunk, hyper::Error>>, hyper::Error>>>>` cannot be sent between threads safely
|
= help: within `hyper::Client<tokio_core::reactor::Handle>`, the trait `std::marker::Send` is not implemented for `std::rc::Rc<std::cell::RefCell<hyper::client::pool::PoolInner<tokio_proto::util::client_proxy::ClientProxy<tokio_proto::streaming::message::Message<hyper::proto::MessageHead<hyper::proto::RequestLine>, hyper::Body>, tokio_proto::streaming::message::Message<hyper::proto::MessageHead<hyper::proto::RawStatus>, tokio_proto::streaming::body::Body<hyper::Chunk, hyper::Error>>, hyper::Error>>>>`
= note: required because it appears within the type `hyper::client::pool::Pool<tokio_proto::util::client_proxy::ClientProxy<tokio_proto::streaming::message::Message<hyper::proto::MessageHead<hyper::proto::RequestLine>, hyper::Body>, tokio_proto::streaming::message::Message<hyper::proto::MessageHead<hyper::proto::RawStatus>, tokio_proto::streaming::body::Body<hyper::Chunk, hyper::Error>>, hyper::Error>>`
= note: required because it appears within the type `hyper::client::Dispatch<hyper::Body>`
= note: required because it appears within the type `hyper::Client<tokio_core::reactor::Handle>`
= note: required by `rocket::State`

error[E0277]: the trait bound `std::rc::Rc<std::cell::RefCell<hyper::client::pool::PoolInner<std::cell::RefCell<futures::sync::mpsc::Sender<(hyper::proto::MessageHead<hyper::proto::RequestLine>, std::option::Option<hyper::Body>, futures::Sender<std::result::Result<hyper::Response, hyper::Error>>)>>>>>: std::marker::Send` is not satisfied in `hyper::Client<tokio_core::reactor::Handle>`
--> src/main.rs:23:1
|
23 | / fn index(var: String, client: State<hyper::Client<tokio_core::reactor::Handle>>) -> &'static str {
24 | | let json = r#"{"library":"hyper"}"#;
25 | | let uri = "http://xxx.xxx.xxx.xxx".parse().unwrap();
26 | | let mut req = Request::new(Method::Post, uri);
... |
33 | | });
34 | | }
| |_^ `std::rc::Rc<std::cell::RefCell<hyper::client::pool::PoolInner<std::cell::RefCell<futures::sync::mpsc::Sender<(hyper::proto::MessageHead<hyper::proto::RequestLine>, std::option::Option<hyper::Body>, futures::Sender<std::result::Result<hyper::Response, hyper::Error>>)>>>>>` cannot be sent between threads safely
|
= help: within `hyper::Client<tokio_core::reactor::Handle>`, the trait `std::marker::Send` is not implemented for `std::rc::Rc<std::cell::RefCell<hyper::client::pool::PoolInner<std::cell::RefCell<futures::sync::mpsc::Sender<(hyper::proto::MessageHead<hyper::proto::RequestLine>, std::option::Option<hyper::Body>, futures::Sender<std::result::Result<hyper::Response, hyper::Error>>)>>>>>`
= note: required because it appears within the type `hyper::client::pool::Pool<std::cell::RefCell<futures::sync::mpsc::Sender<(hyper::proto::MessageHead<hyper::proto::RequestLine>, std::option::Option<hyper::Body>, futures::Sender<std::result::Result<hyper::Response, hyper::Error>>)>>>`
= note: required because it appears within the type `hyper::client::Dispatch<hyper::Body>`
= note: required because it appears within the type `hyper::Client<tokio_core::reactor::Handle>`
= note: required by `rocket::State`

error[E0277]: the trait bound `std::rc::Rc<std::cell::RefCell<hyper::client::pool::PoolInner<tokio_proto::util::client_proxy::ClientProxy<tokio_proto::streaming::message::Message<hyper::proto::MessageHead<hyper::proto::RequestLine>, hyper::Body>, tokio_proto::streaming::message::Message<hyper::proto::MessageHead<hyper::proto::RawStatus>, tokio_proto::streaming::body::Body<hyper::Chunk, hyper::Error>>, hyper::Error>>>>: std::marker::Sync` is not satisfied in `hyper::Client<tokio_core::reactor::Handle>`
--> src/main.rs:23:1
|
23 | / fn index(var: String, client: State<hyper::Client<tokio_core::reactor::Handle>>) -> &'static str {
24 | | let json = r#"{"library":"hyper"}"#;
25 | | let uri = "http://xxx.xxx.xxx.xxx".parse().unwrap();
26 | | let mut req = Request::new(Method::Post, uri);
... |
33 | | });
34 | | }
| |_^ `std::rc::Rc<std::cell::RefCell<hyper::client::pool::PoolInner<tokio_proto::util::client_proxy::ClientProxy<tokio_proto::streaming::message::Message<hyper::proto::MessageHead<hyper::proto::RequestLine>, hyper::Body>, tokio_proto::streaming::message::Message<hyper::proto::MessageHead<hyper::proto::RawStatus>, tokio_proto::streaming::body::Body<hyper::Chunk, hyper::Error>>, hyper::Error>>>>` cannot be shared between threads safely
|
= help: within `hyper::Client<tokio_core::reactor::Handle>`, the trait `std::marker::Sync` is not implemented for `std::rc::Rc<std::cell::RefCell<hyper::client::pool::PoolInner<tokio_proto::util::client_proxy::ClientProxy<tokio_proto::streaming::message::Message<hyper::proto::MessageHead<hyper::proto::RequestLine>, hyper::Body>, tokio_proto::streaming::message::Message<hyper::proto::MessageHead<hyper::proto::RawStatus>, tokio_proto::streaming::body::Body<hyper::Chunk, hyper::Error>>, hyper::Error>>>>`
= note: required because it appears within the type `hyper::client::pool::Pool<tokio_proto::util::client_proxy::ClientProxy<tokio_proto::streaming::message::Message<hyper::proto::MessageHead<hyper::proto::RequestLine>, hyper::Body>, tokio_proto::streaming::message::Message<hyper::proto::MessageHead<hyper::proto::RawStatus>, tokio_proto::streaming::body::Body<hyper::Chunk, hyper::Error>>, hyper::Error>>`
= note: required because it appears within the type `hyper::client::Dispatch<hyper::Body>`
= note: required because it appears within the type `hyper::Client<tokio_core::reactor::Handle>`
= note: required by `rocket::State`

error[E0277]: the trait bound `std::rc::Rc<std::cell::RefCell<hyper::client::pool::PoolInner<std::cell::RefCell<futures::sync::mpsc::Sender<(hyper::proto::MessageHead<hyper::proto::RequestLine>, std::option::Option<hyper::Body>, futures::Sender<std::result::Result<hyper::Response, hyper::Error>>)>>>>>: std::marker::Sync` is not satisfied in `hyper::Client<tokio_core::reactor::Handle>`
--> src/main.rs:23:1
|
23 | / fn index(var: String, client: State<hyper::Client<tokio_core::reactor::Handle>>) -> &'static str {
24 | | let json = r#"{"library":"hyper"}"#;
25 | | let uri = "http://xxx.xxx.xxx.xxx".parse().unwrap();
26 | | let mut req = Request::new(Method::Post, uri);
... |
33 | | });
34 | | }
| |_^ `std::rc::Rc<std::cell::RefCell<hyper::client::pool::PoolInner<std::cell::RefCell<futures::sync::mpsc::Sender<(hyper::proto::MessageHead<hyper::proto::RequestLine>, std::option::Option<hyper::Body>, futures::Sender<std::result::Result<hyper::Response, hyper::Error>>)>>>>>` cannot be shared between threads safely
|
= help: within `hyper::Client<tokio_core::reactor::Handle>`, the trait `std::marker::Sync` is not implemented for `std::rc::Rc<std::cell::RefCell<hyper::client::pool::PoolInner<std::cell::RefCell<futures::sync::mpsc::Sender<(hyper::proto::MessageHead<hyper::proto::RequestLine>, std::option::Option<hyper::Body>, futures::Sender<std::result::Result<hyper::Response, hyper::Error>>)>>>>>`
= note: required because it appears within the type `hyper::client::pool::Pool<std::cell::RefCell<futures::sync::mpsc::Sender<(hyper::proto::MessageHead<hyper::proto::RequestLine>, std::option::Option<hyper::Body>, futures::Sender<std::result::Result<hyper::Response, hyper::Error>>)>>>`
= note: required because it appears within the type `hyper::client::Dispatch<hyper::Body>`
= note: required because it appears within the type `hyper::Client<tokio_core::reactor::Handle>`
= note: required by `rocket::State`

我的状态有问题吗?请不要为答案付出太多努力。如果你能给我指出正确的方向,我可以自己工作。另一种解决方案如何使用另一种设置来解决这个问题也非常受欢迎。

最佳答案

不,您不能在线程之间共享 hyper::Client。但是,您可以共享一个 tokio_core Remote :

#![feature(plugin, decl_macro)]
#![plugin(rocket_codegen)]
extern crate futures;
extern crate hyper;
extern crate rocket;
extern crate tokio_core;

use rocket::State;
use futures::{Future, Stream};
use tokio_core::reactor::Core;

fn main() {
let core = Core::new().unwrap();
rocket::ignite()
.mount("/", routes![index])
.manage(core.remote())
.launch();
}

#[post("/", data = "<var>")]
fn index(var: String, remote: State<tokio_core::reactor::Remote>) -> String {
remote.spawn(|handle| {
use hyper::{Client, Request, Method};
use hyper::header::{ContentLength, ContentType};

let client = Client::new(handle);

let json = r#"{"library":"hyper"}"#;
let uri = "http://xxx.xxx.xxx.xxx".parse().unwrap();
let mut req = Request::new(Method::Post, uri);
req.headers_mut().set(ContentType::json());
req.headers_mut().set(ContentLength(json.len() as u64));
req.set_body(json);

client.request(req)
.and_then(|res| res.body().concat2())
.map(|_| ())
.map_err(|_| ())
});

var
}

然后问题转向你的另一个断言:

I would have to create them each time Rocket ignites a request. This would take too many resources in an high performance environment

我不确定您做出这样的决定的依据是什么。 Constructing a Client doesn't do much .

关于rust - 我可以在 Rocket 中重复使用 Tokio Core 和 Hyper Client 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47542326/

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