Skip to content

Commit

Permalink
fix(server): Make sleep_on_errors configurable and use it in example
Browse files Browse the repository at this point in the history
  • Loading branch information
klausi committed Feb 24, 2018
1 parent 68458cd commit 3a36eb5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
5 changes: 4 additions & 1 deletion examples/hello.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
13 changes: 10 additions & 3 deletions src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,7 @@ impl Stream for AddrIncoming {
type Error = ::std::io::Error;

fn poll(&mut self) -> Poll<Option<Self::Item>, 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(_) => {}
Expand All @@ -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);
Expand All @@ -698,7 +704,8 @@ impl Stream for AddrIncoming {
return Ok(Async::NotReady);
}
}
}
},
Err(e) => return Err(e),
}
}
}
Expand Down

0 comments on commit 3a36eb5

Please sign in to comment.