Skip to content

Commit 48fc125

Browse files
committed
feat(server): Make sleep_on_error duration configurable and enable it per default
1 parent d58aa73 commit 48fc125

File tree

2 files changed

+30
-28
lines changed

2 files changed

+30
-28
lines changed

examples/hello.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ fn main() {
2020
}));
2121

2222
let server = Http::new()
23-
.sleep_on_errors(true)
2423
.bind(&addr, new_service)
2524
.unwrap();
2625
println!("Listening on http://{} with 1 thread.", server.local_addr().unwrap());

src/server/mod.rs

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ pub struct Http<B = ::Chunk> {
5959
max_buf_size: Option<usize>,
6060
keep_alive: bool,
6161
pipeline: bool,
62-
sleep_on_errors: bool,
62+
sleep_on_errors: Option<Duration>,
6363
_marker: PhantomData<B>,
6464
}
6565

@@ -106,7 +106,7 @@ pub struct AddrIncoming {
106106
keep_alive_timeout: Option<Duration>,
107107
listener: TcpListener,
108108
handle: Handle,
109-
sleep_on_errors: bool,
109+
sleep_on_errors: Option<Duration>,
110110
timeout: Option<Timeout>,
111111
}
112112

@@ -121,7 +121,7 @@ impl<B: AsRef<[u8]> + 'static> Http<B> {
121121
keep_alive: true,
122122
max_buf_size: None,
123123
pipeline: false,
124-
sleep_on_errors: false,
124+
sleep_on_errors: Some(Duration::from_millis(10)),
125125
_marker: PhantomData,
126126
}
127127
}
@@ -156,9 +156,11 @@ impl<B: AsRef<[u8]> + 'static> Http<B> {
156156
/// (like "too many files open") may consume 100% CPU and a timout of 10ms
157157
/// is used in that case.
158158
///
159-
/// Default is false.
160-
pub fn sleep_on_errors(&mut self, enabled: bool) -> &mut Self {
161-
self.sleep_on_errors = enabled;
159+
/// This can be disabled by setting the duration to None.
160+
///
161+
/// Default is 10ms.
162+
pub fn sleep_on_errors(&mut self, duration: Option<Duration>) -> &mut Self {
163+
self.sleep_on_errors = duration;
162164
self
163165
}
164166

@@ -544,7 +546,7 @@ where
544546
// ===== impl AddrIncoming =====
545547

546548
impl AddrIncoming {
547-
fn new(listener: TcpListener, handle: Handle, sleep_on_errors: bool) -> io::Result<AddrIncoming> {
549+
fn new(listener: TcpListener, handle: Handle, sleep_on_errors: Option<Duration>) -> io::Result<AddrIncoming> {
548550
Ok(AddrIncoming {
549551
addr: listener.local_addr()?,
550552
keep_alive_timeout: None,
@@ -590,29 +592,30 @@ impl Stream for AddrIncoming {
590592
return Ok(Async::Ready(Some(AddrStream::new(socket, addr))));
591593
},
592594
Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => return Ok(Async::NotReady),
593-
Err(ref e) if self.sleep_on_errors => {
594-
// Connection errors can be ignored directly, continue by
595-
// accepting the next request.
596-
if connection_error(e) {
597-
continue;
598-
}
599-
// Sleep 10ms.
600-
let delay = ::std::time::Duration::from_millis(10);
601-
debug!("accept error: {}; sleeping {:?}",
602-
e, delay);
603-
let mut timeout = Timeout::new(delay, &self.handle)
604-
.expect("can always set a timeout");
605-
let result = timeout.poll()
606-
.expect("timeout never fails");
607-
match result {
608-
Async::Ready(()) => continue,
609-
Async::NotReady => {
610-
self.timeout = Some(timeout);
611-
return Ok(Async::NotReady);
595+
Err(e) => {
596+
if let Some(delay) = self.sleep_on_errors {
597+
// Connection errors can be ignored directly, continue
598+
// by accepting the next request.
599+
if connection_error(&e) {
600+
continue;
601+
}
602+
// Sleep for a short duration (default: 10ms).
603+
debug!("accept error: {}; sleeping {:?}",
604+
e, delay);
605+
let mut timeout = Timeout::new(delay, &self.handle)
606+
.expect("can always set a timeout");
607+
let result = timeout.poll()
608+
.expect("timeout never fails");
609+
match result {
610+
Async::Ready(()) => continue,
611+
Async::NotReady => {
612+
self.timeout = Some(timeout);
613+
return Ok(Async::NotReady);
614+
}
612615
}
613616
}
617+
return Err(e);
614618
},
615-
Err(e) => return Err(e),
616619
}
617620
}
618621
}

0 commit comments

Comments
 (0)