Search code examples
asynchronousrustasync-awaitfuturehyper

How to write a simple Rust asynchronous proxy using futures "0.3" and hyper "0.13.0-alpha.4"?


I am trying to rewrite the proxy example of Asynchronous Programming in Rust book by migrating to :

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

from:

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

The current example converts the returned Future from a futures 0.3 into a futures 0.1, because hyper = "0.12.9" is not compatible with futures 0.3's async/await.

My code:

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);
}

First, I receive this error for server in run_server function:

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>

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

I will be grateful if you tell me your ideas on how to fix it.


Solution

  • By this issue, to create a new service for each connection you need to create MakeService in hyper = "0.13.0-alpha.4". You can create MakeService with a closure by using make_service_fn.

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

    Correct, under the hood hyper::rt::run was calling tokio::run, it has been removed from the api but currently i don't know the reason. You can run your future with calling tokio::run by yourself or use #[tokio::main] annotation. To do this you need to add tokio to your cargo:

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

    then change your run_server like this:

    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);
        }
    }
    

    and main :

    #[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
    }