From 3a36eb559676349d8a321c3159684503014f7fbe Mon Sep 17 00:00:00 2001 From: Klaus Purer Date: Sat, 24 Feb 2018 16:19:01 +0100 Subject: [PATCH] fix(server): Make sleep_on_errors configurable and use it in example --- examples/hello.rs | 5 ++++- src/server/mod.rs | 13 ++++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/examples/hello.rs b/examples/hello.rs index 208d05c90f..8636bdbad0 100644 --- a/examples/hello.rs +++ b/examples/hello.rs @@ -19,7 +19,10 @@ fn main() { .with_body(PHRASE)) })); - let server = Http::new().bind(&addr, new_service).unwrap(); + let server = Http::new() + .sleep_on_errors(true) + .bind(&addr, new_service) + .unwrap(); println!("Listening on http://{} with 1 thread.", server.local_addr().unwrap()); server.run().unwrap(); } diff --git a/src/server/mod.rs b/src/server/mod.rs index b8abbbbd45..aa71b00152 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -664,6 +664,7 @@ impl Stream for AddrIncoming { type Error = ::std::io::Error; fn poll(&mut self) -> Poll, Self::Error> { + // Check if a previous timeout is active that was set by IO errors. if let Some(ref mut to) = self.timeout { match to.poll().expect("timeout never fails") { Async::Ready(_) => {} @@ -682,8 +683,13 @@ impl Stream for AddrIncoming { return Ok(Async::Ready(Some(AddrStream::new(socket, addr)))); }, Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => return Ok(Async::NotReady), - Err(ref e) if connection_error(e) => continue, - Err(e) => { + Err(ref e) if self.sleep_on_errors => { + // Connection errors can be ignored directly, continue by + // accepting the next request. + if connection_error(e) { + continue; + } + // Sleep 10ms. let delay = ::std::time::Duration::from_millis(10); debug!("Accept error: {}. Sleeping {:?}...", e, delay); @@ -698,7 +704,8 @@ impl Stream for AddrIncoming { return Ok(Async::NotReady); } } - } + }, + Err(e) => return Err(e), } } }