@@ -59,7 +59,7 @@ pub struct Http<B = ::Chunk> {
59
59
max_buf_size : Option < usize > ,
60
60
keep_alive : bool ,
61
61
pipeline : bool ,
62
- sleep_on_errors : bool ,
62
+ sleep_on_errors : Option < Duration > ,
63
63
_marker : PhantomData < B > ,
64
64
}
65
65
@@ -106,7 +106,7 @@ pub struct AddrIncoming {
106
106
keep_alive_timeout : Option < Duration > ,
107
107
listener : TcpListener ,
108
108
handle : Handle ,
109
- sleep_on_errors : bool ,
109
+ sleep_on_errors : Option < Duration > ,
110
110
timeout : Option < Timeout > ,
111
111
}
112
112
@@ -121,7 +121,7 @@ impl<B: AsRef<[u8]> + 'static> Http<B> {
121
121
keep_alive : true ,
122
122
max_buf_size : None ,
123
123
pipeline : false ,
124
- sleep_on_errors : false ,
124
+ sleep_on_errors : Some ( Duration :: from_millis ( 10 ) ) ,
125
125
_marker : PhantomData ,
126
126
}
127
127
}
@@ -156,9 +156,11 @@ impl<B: AsRef<[u8]> + 'static> Http<B> {
156
156
/// (like "too many files open") may consume 100% CPU and a timout of 10ms
157
157
/// is used in that case.
158
158
///
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;
162
164
self
163
165
}
164
166
@@ -544,7 +546,7 @@ where
544
546
// ===== impl AddrIncoming =====
545
547
546
548
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 > {
548
550
Ok ( AddrIncoming {
549
551
addr : listener. local_addr ( ) ?,
550
552
keep_alive_timeout : None ,
@@ -590,29 +592,30 @@ impl Stream for AddrIncoming {
590
592
return Ok ( Async :: Ready ( Some ( AddrStream :: new ( socket, addr) ) ) ) ;
591
593
} ,
592
594
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
+ }
612
615
}
613
616
}
617
+ return Err ( e) ;
614
618
} ,
615
- Err ( e) => return Err ( e) ,
616
619
}
617
620
}
618
621
}
0 commit comments