Skip to content

Commit

Permalink
Use rustls::Writer::write_vectored to reduce writev syscalls
Browse files Browse the repository at this point in the history
Signed-off-by: Eloi DEMOLIS <eloi.demolis@clever-cloud.com>
  • Loading branch information
Wonshtrum committed Nov 8, 2023
1 parent 71ca2a8 commit 8364454
Showing 1 changed file with 63 additions and 20 deletions.
83 changes: 63 additions & 20 deletions lib/src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ pub trait SocketHandler {
fn socket_write_vectored(&mut self, _buf: &[std::io::IoSlice]) -> (usize, SocketResult) {
unimplemented!()
}
fn has_vectored_writes(&self) -> bool {
false
}
fn socket_ref(&self) -> &TcpStream;
fn socket_mut(&mut self) -> &mut TcpStream;
fn protocol(&self) -> TransportProtocol;
Expand Down Expand Up @@ -135,10 +132,6 @@ impl SocketHandler for TcpStream {
}
}

fn has_vectored_writes(&self) -> bool {
true
}

fn socket_ref(&self) -> &TcpStream {
self
}
Expand Down Expand Up @@ -339,22 +332,72 @@ impl SocketHandler for FrontRustls {
}
}

fn has_vectored_writes(&self) -> bool {
true
}

fn socket_write_vectored(&mut self, bufs: &[std::io::IoSlice]) -> (usize, SocketResult) {
let mut total_size = 0;
let mut socket_state = SocketResult::Continue;
let mut size;
for buf in bufs {
(size, socket_state) = self.socket_write(buf);
total_size += size;
if socket_state != SocketResult::Continue {
break;
let mut buffered_size = 0usize;
let mut can_write = true;
let mut is_error = false;
let mut is_closed = false;

match self.session.writer().write_vectored(&bufs) {
Ok(0) => {}
Ok(sz) => {
buffered_size += sz;
}
Err(e) => match e.kind() {
ErrorKind::WouldBlock => {
// we don't need to do anything, the session will return false in wants_write?
//error!("rustls socket_write wouldblock");
}
ErrorKind::ConnectionReset
| ErrorKind::ConnectionAborted
| ErrorKind::BrokenPipe => {
//FIXME: this should probably not happen here
incr!("rustls.write.error");
is_closed = true;
}
_ => {
error!("could not write data to TLS stream: {:?}", e);
incr!("rustls.write.error");
is_error = true;
}
},
}

loop {
match self.session.write_tls(&mut self.stream) {
Ok(0) => {
//can_write = false;
break;
}
Ok(_sz) => {}
Err(e) => match e.kind() {
ErrorKind::WouldBlock => can_write = false,
ErrorKind::ConnectionReset
| ErrorKind::ConnectionAborted
| ErrorKind::BrokenPipe => {
incr!("rustls.write.error");
is_closed = true;
break;
}
_ => {
error!("could not write TLS stream to socket: {:?}", e);
incr!("rustls.write.error");
is_error = true;
break;
}
},
}
}

if is_error {
(buffered_size, SocketResult::Error)
} else if is_closed {
(buffered_size, SocketResult::Closed)
} else if !can_write {
(buffered_size, SocketResult::WouldBlock)
} else {
(buffered_size, SocketResult::Continue)
}
(total_size, socket_state)
}

fn socket_ref(&self) -> &TcpStream {
Expand Down

0 comments on commit 8364454

Please sign in to comment.