Add some educational comments

This commit is contained in:
Mahmoud Al-Qudsi 2023-08-29 13:42:09 -05:00
parent 55b654ebd1
commit a716d0c3c0
1 changed files with 7 additions and 0 deletions

View File

@ -94,6 +94,11 @@ async fn forward(bind_ip: &str, local_port: i32, remote: String) -> Result<(), B
// (This reduces MESI/MOESI cache traffic between CPU cores.) // (This reduces MESI/MOESI cache traffic between CPU cores.)
let remote: &str = Box::leak(remote.into_boxed_str()); let remote: &str = Box::leak(remote.into_boxed_str());
// Two instances of this function are spawned for each half of the connection: client-to-server,
// server-to-client. We can't use tokio::io::copy() instead (no matter how convenient it might
// be) because it doesn't give us a way to correlate the lifetimes of the two tcp read/write
// loops: even after the client disconnects, tokio would keep the upstream connection to the
// server alive until the connection's max client idle timeout is reached.
async fn copy_with_abort<R, W>( async fn copy_with_abort<R, W>(
read: &mut R, read: &mut R,
write: &mut W, write: &mut W,
@ -127,6 +132,8 @@ async fn forward(bind_ip: &str, local_port: i32, remote: String) -> Result<(), B
break; break;
} }
// While we ignore some read errors above, any error writing data we've already read to
// the other side is always treated as exceptional.
write.write_all(&buf[0..bytes_read]).await?; write.write_all(&buf[0..bytes_read]).await?;
copied += bytes_read; copied += bytes_read;
} }