Skip to content

Commit eeb3bd6

Browse files
committed
fix relay half-close data loss (closes #6)
1 parent 76e3f86 commit eeb3bd6

1 file changed

Lines changed: 4 additions & 25 deletions

File tree

src/relay.rs

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,8 @@
1-
use tokio::io;
1+
use tokio::io::copy_bidirectional;
22
use tokio::net::TcpStream;
33

4-
pub async fn relay(client: TcpStream, upstream: TcpStream) -> Result<(), std::io::Error> {
5-
let (mut cr, mut cw) = io::split(client);
6-
let (mut ur, mut uw) = io::split(upstream);
7-
8-
let c2u = tokio::spawn(async move { io::copy(&mut cr, &mut uw).await });
9-
let u2c = tokio::spawn(async move { io::copy(&mut ur, &mut cw).await });
10-
11-
tokio::select! {
12-
r = c2u => {
13-
match r {
14-
Ok(Ok(n)) => tracing::debug!(bytes = n, "client->upstream finished"),
15-
Ok(Err(e)) => tracing::debug!("client->upstream error: {}", e),
16-
Err(e) => tracing::debug!("client->upstream task panicked: {}", e),
17-
}
18-
}
19-
r = u2c => {
20-
match r {
21-
Ok(Ok(n)) => tracing::debug!(bytes = n, "upstream->client finished"),
22-
Ok(Err(e)) => tracing::debug!("upstream->client error: {}", e),
23-
Err(e) => tracing::debug!("upstream->client task panicked: {}", e),
24-
}
25-
}
26-
}
27-
4+
pub async fn relay(mut client: TcpStream, mut upstream: TcpStream) -> Result<(), std::io::Error> {
5+
let (c2u, u2c) = copy_bidirectional(&mut client, &mut upstream).await?;
6+
tracing::debug!(c2u, u2c, "relay finished");
287
Ok(())
298
}

0 commit comments

Comments
 (0)