Rust 我可以在火箭中重复使用Tokio核心和超级客户端吗?

Rust 我可以在火箭中重复使用Tokio核心和超级客户端吗?,rust,hyper,Rust,Hyper,我想在收到来自用户的传入请求后,使用Hyper 0.11发出客户端请求,该请求由我使用rocket处理,并使用尽可能少的资源。如果我不能重用核心或客户端,我就必须在每次火箭触发请求时创建它们。在高性能环境中,每次服务器应答请求时,创建核心和客户端都需要太多资源。因此,我使用状态使对象可用于所有“点火”。在Hyper 0.10.13中,它可以工作,但在带有东京的Hyper 0.11中,我收到了大量错误消息 Cargo.toml: rocket = "0.3.3" rocket_codegen =

我想在收到来自用户的传入请求后,使用Hyper 0.11发出客户端请求,该请求由我使用rocket处理,并使用尽可能少的资源。如果我不能重用
核心
客户端
,我就必须在每次火箭触发请求时创建它们。在高性能环境中,每次服务器应答请求时,创建
核心
客户端
都需要太多资源。因此,我使用
状态
使对象可用于所有“点火”。在Hyper 0.10.13中,它可以工作,但在带有东京的Hyper 0.11中,我收到了大量错误消息

Cargo.toml:

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

#![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()
    });
}
#![功能(插件、decl_宏)]
#![插件(rocket_codegen)]
外部板条箱期货;
外部板条箱;
外部板条箱火箭;
外部板条箱东京大学核心;
使用火箭::状态;
使用超链接::*;
使用hyper::header::{ContentLength,ContentType};
使用未来:{Future,Stream};
使用tokio_堆芯::反应堆::堆芯;
fn main(){
让mut core=core::new().unwrap();
让client=client::new(&core.handle());
火箭:点火
.mount(“/”,routes![索引])
.manage(client.clone())
.launch();
}
#[发布(“/”,数据=”)]
fn索引(变量:字符串,客户端:状态)->&'static str{
让json=r#“{”library:“hyper”}”;
让uri=”http://xxx.xxx.xxx.xxx“.parse().unwrap();
让mut-req=Request::new(方法::Post,uri);
req.headers_mut().set(ContentType::json());
req.headers_mut().set(ContentLength(json.len()作为u64));
请求集合体(json);
让post=client.request(req.),然后(| res |{
//println!(“POST:{}”,res.status());
res.body().concat2()
});
}
我收到的错误消息如下:

error[E0277]:在'hyper::Client'中未满足特性绑定'std::rc::弱:std::marker::Send'`
-->src/main.rs:23:1
|
23 |/fn索引(变量:字符串,客户端:状态)->&'static str{
24 | |让json=r#“{”library:“hyper”}”;
25 | |让uri=”http://xxx.xxx.xxx.xxx“.parse().unwrap();
26 | |让mut req=Request::new(方法::Post,uri);
...  |
33 | |     });
34 | | }
|| ^`std::rc::Weak`无法在线程之间安全发送
|
=帮助:在“hyper::Client”中,“std::marker::Send”特性未为“std::rc::Weak”实现`
=注意:必需,因为它出现在类型“tokio_core::reactor::Handle”中`
=注意:必需,因为它出现在“hyper::Client”类型中`
=注意:`rocket::State'需要`
错误[E0277]:在'hyper::Client'中不满足特性绑定'std::rc::Weak:std::marker::Sync'`
-->src/main.rs:23:1
|
23 |/fn索引(变量:字符串,客户端:状态)->&'static str{
24 | |让json=r#“{”library:“hyper”}”;
25 | |让uri=”http://xxx.xxx.xxx.xxx“.parse().unwrap();
26 | |让mut req=Request::new(方法::Post,uri);
...  |
33 | |     });
34 | | }
|| ^`std::rc::Weak`无法在线程之间安全共享
|
=帮助:在“hyper::Client”中,“std::marker::Sync”特性没有为“std::rc::Weak”实现`
=注意:必需,因为它出现在类型“tokio_core::reactor::Handle”中`
=注意:必需,因为它出现在“hyper::Client”类型中`
=注意:`rocket::State'需要`
错误[E0277]:在'hyper::Client'中未满足特性绑定'std::rc::rc:std::marker::Send'`
-->src/main.rs:23:1
|
23 |/fn索引(变量:字符串,客户端:状态)->&'static str{
24 | |让json=r#“{”library:“hyper”}”;
25 | |让uri=”http://xxx.xxx.xxx.xxx“.parse().unwrap();
26 | |让mut req=Request::new(方法::Post,uri);
...  |
33 | |     });
34 | | }
|| ^`std::rc::rc`无法在线程之间安全发送
|
=帮助:在“hyper::Client”中,“std::marker::Send”特性没有为“std::rc::rc”实现`
=注意:必需,因为它出现在类型“hyper::client::pool::pool”中`
=注意:必需,因为它出现在类型“hyper::client::Dispatch”中`
=注意:必需,因为它出现在“hyper::Client”类型中`
=注意:`rocket::State'需要`
错误[E0277]:在'hyper::Client'中未满足特性绑定'std::rc::rc:std::marker::Send'`
-->src/main.rs:23:1
|
23 |/fn索引(变量:字符串,客户端:状态)->&'static str{
24 | |让json=r#“{”library:“hyper”}”;
25 | |让uri=”http://xxx.xxx.xxx.xxx“.parse().unwrap();
26 | |让mut req=Request::new(方法::Post,uri);
...  |
33 | |     });
34 | | }
|| ^`std::rc::rc`无法在线程之间安全发送
|
=帮助:在“hyper::Client”中,“std::marker::Send”特性没有为“std::rc::rc”实现`
=注意:必需,因为它出现在类型“hyper::client::pool::pool”中`
=注意:必需,因为它出现在类型“hyper::client::Dispatch”中`
=注意:必需,因为它出现在“hyper::Client”类型中`
=注意:`rocket::State'需要`
错误[E0277]:在'hyper::Client'中不满足特性绑定'std::rc::rc:std::marker::Sync'`
-->src/main.rs:23:1
|
23 |/fn索引(变量:字符串,客户端:状态)->&'static str{
24 | |让json=r#“{”library:“hyper”}”;
25 | |让uri=”http://xxx.xxx.xxx.xxx“.parse().unwrap();
26 | |让mut req=Request::new(方法::Post,uri);
...  |
33 | |     });
34 | | }
|| ^`std::rc::rc`无法在线程之间安全共享
|
=帮助:在“hyper::Client”中,“std::marker::Sync”特性没有为“std::rc::rc”实现`
=注意:必需,因为它出现在类型“hyper::client::pool::pool”中`
=注意:必需,因为它出现在类型“hyper::client::Dispatch”中`
=注意:必需,因为它出现在“hyper::Client”类型中`
=注意:`rocket::State'需要`
错误[E0277]:在'hyper::Client'中不满足特性绑定'std::rc::rc:std::marker::Sync'`
-->src/main.rs:23:1
|
23 |/fn索引(变量:字符串,客户端:状态)->&'static str{
24 |
#![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
}