基于https://github.com/tokio-rs/tokio/blob/master/examples/proxy.rs上的 tokio 示例
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(())
我有一个修改版本,以便我可以处理每个连接的终止,如下所示:
// 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 {...}
这使我能够正确计时客户端的连接时间,因为客户端在收到答案后立即关闭连接,而代理服务器似乎需要(在我的情况下为 8 秒)关闭。鉴于这种代码结构,一旦我退出 client_to_server 的未来(即不等待我观察到它需要关闭的 8 秒),是否有可能终止来自 server_to_client 的下游连接?
谢谢