Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: Blocking client in future context #342

Conversation

ernestas-poskus
Copy link

examples/hello_world/
$ rustc -V
rustc 1.38.0-nightly (07e0c3651 2019-07-16)
$ cargo run
   Compiling gotham_examples_hello_world v0.0.0 (/rust/gotham/examples/hello_world)
    Finished dev [unoptimized + debuginfo] target(s) in 3.98s
     Running `/rust/gotham/target/debug/gotham_examples_hello_world`
Listening for requests at http://127.0.0.1:7878
Error(
    BlockingClientInFutureContext,
    "https://httpbin.org/ip",
)
Error(
    BlockingClientInFutureContext,
    "https://httpbin.org/ip",
)

Getting rather odd error when doing foreign 'sync' requests using reqwest crate in any handler.

Maybe this issue is related to #307?

Tried to use a custom executor for Gotham server but got the same result.

Maybe someone has had a similar error? Can't find much docs about it or true origin to debug further.

@colinbankier
Copy link
Collaborator

To use reqwest in a future context, use their async client.
Here is their example from https://github.com/seanmonstar/reqwest/blob/master/examples/async.rs translated into a gotham handler:

pub fn say_hello(state: State) -> Box<HandlerFuture> {
    let f = Client::new()
        .get("https://hyper.rs")
        .send()
        .and_then(|mut res| {
            println!("{}", res.status());

            let body = mem::replace(res.body_mut(), Decoder::empty());
            body.concat2()
        })
        .map_err(|err| println!("request error: {}", err))
        .map(|body| {
            let mut body = Cursor::new(body);
            let _ = io::copy(&mut body, &mut io::stdout()).map_err(|err| {
                println!("stdout error: {}", err);
            });
        });

    Box::new(f.then(move |_result| {
        let res = create_response(&state, StatusCode::OK, mime::TEXT_PLAIN, HELLO_WORLD);
        Ok((state, res))
    }))
}

@ernestas-poskus
Copy link
Author

thank you

@ernestas-poskus ernestas-poskus deleted the blocking_client_in_future_context branch August 12, 2019 17:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants