Skip to content

Commit

Permalink
avoid using write_all to ensure select! cancellation safe
Browse files Browse the repository at this point in the history
  • Loading branch information
neevek committed Dec 1, 2024
1 parent f639376 commit ece6484
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "omnip"
version = "0.6.2"
version = "0.6.3"
edition = "2021"

[lib]
Expand Down
29 changes: 10 additions & 19 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -976,32 +976,23 @@ impl Server {
Ok(0)
}
Ok(n) => {
// this method is used as a select! branch, use while loop to write the data
// instead of using write_all (which is not cancellation safe) to ensure
// the code cancellation safe
let mut written_bytes = 0;
while written_bytes < n {
written_bytes += writer
.write(&buffer[written_bytes..n])
.await
.map_err(|_| ProxyError::InternalError)?;
}
*out_bytes += n as u64;
writer
.write_all(&buffer[..n])
.await
.map_err(|_| ProxyError::InternalError)?;
Ok(n)
}
Err(_) => Err(ProxyError::InternalError), // Connection mostly reset by peer
}
}

// async fn resolve_net_addr(&self, addr: &NetAddr) -> Result<SocketAddr> {
// if addr.is_ip() {
// return Ok(addr.to_socket_addr().unwrap());
// }
//
// let resolver = if addr.is_internal_domain() {
// inner_state!(self, system_dns_resolver).clone()
// } else {
// inner_state!(self, dns_resolver).clone()
// };
//
// let ip_arr = resolver.unwrap().lookup(addr.unwrap_domain()).await?;
// Ok(SocketAddr::new(*ip_arr.first().unwrap(), addr.port))
// }

fn collect_and_report_server_stats(&self, mut stats_receiver: Receiver<ServerStats>) {
let inner_state = self.inner_state.clone();
tokio::spawn(async move {
Expand Down

0 comments on commit ece6484

Please sign in to comment.