-1

Based on tokio's example at https://github.com/tokio-rs/tokio/blob/master/examples/proxy.rs

let (mut ri, mut wi) = inbound.split();
let (mut ro, mut wo) = outbound.split();

let client_to_server = io::copy(&mut ri, &mut wo);
let server_to_client = io::copy(&mut ro, &mut wi);

try_join(client_to_server, server_to_client).await?;

Ok(())

I have a modified version so that I can handle the termination of each connection as in:

    // Server will disconnect their side normally 8s later, from what I've observed
    let server_to_client = io::copy(&mut ro, &mut wi).map(|f| {
        server_session_time = server_start_time.elapsed().unwrap();
        f
    });

    // Normally, this will stop first, as the client disconnects as soon as he has the results...
    let client_to_server = io::copy(&mut ri, &mut wo).map(|f| {
        client_session_time = client_start_time.elapsed().unwrap();
        f
    });


    // Join on both
    match try_join(client_to_server, server_to_client).await {...}

This has allowed me to time correctly the connected time for the client side, since the clients immediately close connection upon receiving the answer, while the proxied server seems to take (in my case 8s) to close.

Given this structure of code, is there any possibility to terminate the downstream connection from server_to_client, once I exit the future of the client_to_server (i.e. not wait the 8s that I observe that it takes to be shutdown)?

E_net4
  • 27,810
  • 13
  • 101
  • 139
user2123288
  • 1,103
  • 1
  • 13
  • 22

1 Answers1

-3

Ok with a few more examples, was able to understand what I had to do. For any people coming back to this question in the future, what is needed is that you implement the bidirectional copy yourself based on the 4 futures of each of the reads and writes with tokio::select!. That will allow to access to all the streams and when one of them terminates, it is your option if you want to complete processing the others or just stop. As it is above there is no way to "cancel" the "other" copy...

You can look both at the implementation of io::copy https://github.com/tokio-rs/tokio-io/blob/master/src/copy.rs and tokio::select https://docs.rs/tokio/0.2.20/tokio/macro.select.html, to build your 4-way select.

user2123288
  • 1,103
  • 1
  • 13
  • 22
  • what do you mean 4 way. How is that even possible when you only have 2 pairs of halves – nikoss Oct 21 '20 at 20:53
  • 2 pairs of halves, means that you have two futures pending on read from both hosts, and 2 futures pending on write from the same hosts, therefore making the 3. – user2123288 Oct 22 '20 at 21:18