Skip to content

Http::bind does not scale to uses where a Handle is needed #1263

Closed
@brendanzab

Description

@brendanzab

So, I have a ReverseProxy type that requires a Client in order to be constructed. But in order to do that I need to do the manual set up of the Tokio event loop, because constructing a Client requires access to the Handle, which I can't get when calling Http::bind.

fn run() -> hyper::Result<()> {
    // Set up the Tokio reactor core
    let mut core = Core::new()?;
    let handle = core.handle();

    // Set up a TCP socket to listen to
    let listen_addr = SocketAddr::new(Ipv4Addr::new(127, 0, 0, 1).into(), 8080);
    let listener = TcpListener::bind(&listen_addr, &handle)?;

    // Listen to incoming requests over TCP, and forward them to a new `ReverseProxy`
    let http = Http::new();
    let server = listener.incoming().for_each(|(socket, addr)| {
        let client = Client::new(&handle);
        let service = ReverseProxy::new(client, Some(addr.ip()));
        http.bind_connection(&handle, socket, addr, service);
        Ok(())
    });

    // Start our server on the reactor core
    core.run(server)?;

    Ok(())
}

This gets pretty horrific once I start needing the graceful shutdown behavior of Server::run_until, as shown by hyper-reverse-proxy/examples/extended.rs. I pretty much need to re-implement all of that stuff myself...

Is there a better way to go about this? Or could we have a Http::bind_with_handle which could allow us to pass a handle to our own reactor core into Hyper?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions