diff --git a/actix-http/src/builder.rs b/actix-http/src/builder.rs index 408ee7924..e8a23a9e9 100644 --- a/actix-http/src/builder.rs +++ b/actix-http/src/builder.rs @@ -39,7 +39,7 @@ where #[allow(clippy::new_without_default)] pub fn new() -> Self { HttpServiceBuilder { - keep_alive: KeepAlive::Timeout(5), + keep_alive: KeepAlive::default(), client_timeout: 5000, client_disconnect: 0, secure: false, diff --git a/actix-http/src/config.rs b/actix-http/src/config.rs index 497cd7635..f735276c8 100644 --- a/actix-http/src/config.rs +++ b/actix-http/src/config.rs @@ -18,8 +18,8 @@ pub(crate) const DATE_VALUE_LENGTH: usize = 29; #[derive(Debug, PartialEq, Clone, Copy)] /// Server keep-alive setting pub enum KeepAlive { - /// Keep-alive time in seconds. - Timeout(usize), + /// Keep-alive duration. + Timeout(Duration), /// Rely on OS to shutdown TCP connection. Os, @@ -28,18 +28,23 @@ pub enum KeepAlive { Disabled, } +impl Default for KeepAlive { + fn default() -> Self { + Self::Timeout(Duration::from_secs(5)) + } +} + impl From for KeepAlive { - fn from(keepalive: usize) -> Self { - KeepAlive::Timeout(keepalive) + fn from(ka_secs: usize) -> Self { + KeepAlive::Timeout(Duration::from_secs(ka_secs as u64)) } } impl From> for KeepAlive { - fn from(keepalive: Option) -> Self { - if let Some(keepalive) = keepalive { - KeepAlive::Timeout(keepalive) - } else { - KeepAlive::Disabled + fn from(ka_secs_opt: Option) -> Self { + match ka_secs_opt { + Some(ka_secs) => KeepAlive::Timeout(Duration::from_secs(ka_secs as u64)), + None => KeepAlive::Disabled, } } } @@ -61,7 +66,7 @@ struct Inner { impl Default for ServiceConfig { fn default() -> Self { - Self::new(KeepAlive::Timeout(5), 0, 0, false, None) + Self::new(KeepAlive::default(), 0, 0, false, None) } } @@ -75,13 +80,13 @@ impl ServiceConfig { local_addr: Option, ) -> ServiceConfig { let (keep_alive, ka_enabled) = match keep_alive { - KeepAlive::Timeout(val) => (val as u64, true), - KeepAlive::Os => (0, true), - KeepAlive::Disabled => (0, false), + KeepAlive::Timeout(Duration::ZERO) => (Duration::ZERO, false), + KeepAlive::Timeout(val) => (val, true), + KeepAlive::Os => (Duration::ZERO, true), + KeepAlive::Disabled => (Duration::ZERO, false), }; - let keep_alive = - (ka_enabled && keep_alive > 0).then(|| Duration::from_secs(keep_alive)); + let keep_alive = ka_enabled.then(|| keep_alive); ServiceConfig(Rc::new(Inner { keep_alive, diff --git a/actix-http/src/h1/dispatcher.rs b/actix-http/src/h1/dispatcher.rs index 32f42c3cd..ff60623ae 100644 --- a/actix-http/src/h1/dispatcher.rs +++ b/actix-http/src/h1/dispatcher.rs @@ -1181,7 +1181,7 @@ where #[inline] fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { - log::trace!(target: "", ""); + log::trace!(target: "actix*", ""); log::trace!("enter Dispatcher::poll"); let this = self.as_mut().project(); diff --git a/actix-http/src/h1/dispatcher_tests.rs b/actix-http/src/h1/dispatcher_tests.rs index 46f5b9153..3a7bec6e0 100644 --- a/actix-http/src/h1/dispatcher_tests.rs +++ b/actix-http/src/h1/dispatcher_tests.rs @@ -188,7 +188,13 @@ async fn oneshot_connection() { async fn keep_alive_timeout() { let buf = TestBuffer::new("GET /abcd HTTP/1.1\r\n\r\n"); - let cfg = ServiceConfig::new(KeepAlive::Timeout(1), 100, 0, false, None); + let cfg = ServiceConfig::new( + KeepAlive::Timeout(Duration::from_millis(200)), + 100, + 0, + false, + None, + ); let services = HttpFlow::new(echo_path_service(), ExpectHandler, None); let h1 = Dispatcher::<_, _, _, _, UpgradeHandler>::new( @@ -235,7 +241,7 @@ async fn keep_alive_timeout() { .await; // sleep slightly longer than keep-alive timeout - sleep(Duration::from_millis(1100)).await; + sleep(Duration::from_millis(250)).await; lazy(|cx| { assert!( @@ -261,7 +267,13 @@ async fn keep_alive_timeout() { async fn keep_alive_follow_up_req() { let mut buf = TestBuffer::new("GET /abcd HTTP/1.1\r\n\r\n"); - let cfg = ServiceConfig::new(KeepAlive::Timeout(2), 100, 0, false, None); + let cfg = ServiceConfig::new( + KeepAlive::Timeout(Duration::from_millis(500)), + 100, + 0, + false, + None, + ); let services = HttpFlow::new(echo_path_service(), ExpectHandler, None); let h1 = Dispatcher::<_, _, _, _, UpgradeHandler>::new( @@ -308,7 +320,7 @@ async fn keep_alive_follow_up_req() { .await; // sleep for less than KA timeout - sleep(Duration::from_millis(200)).await; + sleep(Duration::from_millis(100)).await; lazy(|cx| { assert!( diff --git a/actix-http/src/responses/head.rs b/actix-http/src/responses/head.rs index 41d558440..f0623a846 100644 --- a/actix-http/src/responses/head.rs +++ b/actix-http/src/responses/head.rs @@ -208,7 +208,6 @@ mod tests { }; use memchr::memmem; - use tokio::io::{AsyncReadExt, AsyncWriteExt, Interest}; use crate::{ h1::H1Service, @@ -239,14 +238,12 @@ mod tests { }) .await; - let mut stream = tokio::net::TcpStream::connect(srv.addr()).await.unwrap(); - dbg!(stream.ready(Interest::WRITABLE).await.unwrap()); + let mut stream = net::TcpStream::connect(srv.addr()).unwrap(); let _ = stream .write_all(b"GET /camel HTTP/1.1\r\nConnection: Close\r\n\r\n") - .await .unwrap(); - let mut data = vec![0; 1024]; - let _ = stream.read_to_end(&mut data).await.unwrap(); + let mut data = vec![0; 256]; + let _ = stream.read(&mut data).unwrap(); assert_eq!(&data[..17], b"HTTP/1.1 200 OK\r\n"); assert!(memmem::find(&data, b"Foo-Bar").is_some()); assert!(memmem::find(&data, b"foo-bar").is_none()); @@ -259,8 +256,8 @@ mod tests { let _ = stream .write_all(b"GET /lower HTTP/1.1\r\nConnection: Close\r\n\r\n") .unwrap(); - let mut data = vec![0; 1024]; - let _ = stream.read_to_end(&mut data).unwrap(); + let mut data = vec![0; 256]; + let _ = stream.read(&mut data).unwrap(); assert_eq!(&data[..17], b"HTTP/1.1 200 OK\r\n"); assert!(memmem::find(&data, b"Foo-Bar").is_none()); assert!(memmem::find(&data, b"foo-bar").is_some()); diff --git a/actix-http/src/service.rs b/actix-http/src/service.rs index cd2efe678..029fa3672 100644 --- a/actix-http/src/service.rs +++ b/actix-http/src/service.rs @@ -60,7 +60,7 @@ where { /// Create new `HttpService` instance. pub fn new>(service: F) -> Self { - let cfg = ServiceConfig::new(KeepAlive::Timeout(5), 5000, 0, false, None); + let cfg = ServiceConfig::new(KeepAlive::default(), 5000, 0, false, None); HttpService { cfg, diff --git a/src/server.rs b/src/server.rs index ed0c965b3..a318bbda1 100644 --- a/src/server.rs +++ b/src/server.rs @@ -88,7 +88,7 @@ where factory, config: Arc::new(Mutex::new(Config { host: None, - keep_alive: KeepAlive::Timeout(5), + keep_alive: KeepAlive::default(), client_timeout: 5000, client_shutdown: 5000, })),