rustrust-rockethyper

Can I reuse a Tokio Core and a Hyper Client in Rocket?


I want to make a client request with Hyper 0.11 after I get an incoming request from a user which I handle with rocket and use as few resources as possible. If I can't reuse the Core or the Client, I would have to create them each time Rocket ignites a request. This would take too many resources in an high performance environment to create a Core and a Client each time the server answers a request. so I am using the State to make the object available to all 'ignitions'. In Hyper 0.10.13 it works, but in Hyper 0.11 with Tokio I am getting massive error messages.

Cargo.toml:

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

main.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()
    });
}

I am getting error messages like:

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`

Is there something wrong with my state? Please don't take too much effort for the answer. If you can point me in the right direction I can work myself. Another solution how to solve this with another setup is very welcome as well.


Solution

  • No, you cannot share a hyper::Client between threads. However, you can share a 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
    }
    

    Then the question turns to your other assertion:

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

    I'm not sure what you are basing such a decision on. Constructing a Client doesn't do much.