Skip to content

Commit 1a9f264

Browse files
committed
perf(server): try to read from socket at keep-alive
In most situations, this should reduce the number of task wake ups by 1 per request, which can help if reading the request was small.
1 parent dd54f20 commit 1a9f264

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

src/http/conn.rs

+10
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,16 @@ where I: AsyncRead + AsyncWrite,
228228
}
229229

230230
if !self.io.is_read_blocked() {
231+
if self.io.read_buf().is_empty() {
232+
match self.io.read_from_io() {
233+
Ok(Async::Ready(_)) => (),
234+
Ok(Async::NotReady) => return,
235+
Err(e) => {
236+
trace!("maybe_notify read_from_io error: {}", e);
237+
self.state.close();
238+
}
239+
}
240+
}
231241
if let Some(ref task) = self.state.read_task {
232242
task.notify();
233243
}

src/http/io.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,13 @@ impl<T: AsyncRead + AsyncWrite> Buffered<T> {
9292
}
9393
}
9494

95-
fn read_from_io(&mut self) -> Poll<usize, io::Error> {
95+
pub fn read_from_io(&mut self) -> Poll<usize, io::Error> {
9696
use bytes::BufMut;
97-
// TODO: Investigate if we still need these unsafe blocks
97+
self.read_blocked = false;
98+
//TODO: use io.read_buf(), so we don't have to zero memory
99+
//Reason this doesn't use it yet is because benchmarks show the
100+
//slightest **decrease** in performance. Switching should be done
101+
//when it doesn't cost anything.
98102
if self.read_buf.remaining_mut() < INIT_BUFFER_SIZE {
99103
self.read_buf.reserve(INIT_BUFFER_SIZE);
100104
unsafe { // Zero out unused memory
@@ -103,13 +107,11 @@ impl<T: AsyncRead + AsyncWrite> Buffered<T> {
103107
ptr::write_bytes(buf.as_mut_ptr(), 0, len);
104108
}
105109
}
106-
self.read_blocked = false;
107-
unsafe { // Can we use AsyncRead::read_buf instead?
110+
unsafe {
108111
let n = match self.io.read(self.read_buf.bytes_mut()) {
109112
Ok(n) => n,
110113
Err(e) => {
111114
if e.kind() == io::ErrorKind::WouldBlock {
112-
// TODO: Push this out, ideally, into http::Conn.
113115
self.read_blocked = true;
114116
return Ok(Async::NotReady);
115117
}

0 commit comments

Comments
 (0)