From e9aabbfd4f511170aa96142e07685c9fb0669369 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Sat, 29 Jan 2022 04:24:06 +0000 Subject: [PATCH] fix keepalive wake up --- actix-http/src/config.rs | 3 +-- actix-http/src/h1/dispatcher.rs | 23 +++++++++++++++++------ actix-http/src/h1/dispatcher_tests.rs | 2 -- actix-http/src/responses/head.rs | 2 -- actix-http/tests/test_server.rs | 15 ++++++++++++--- 5 files changed, 30 insertions(+), 15 deletions(-) diff --git a/actix-http/src/config.rs b/actix-http/src/config.rs index f735276c8..d67f63fa5 100644 --- a/actix-http/src/config.rs +++ b/actix-http/src/config.rs @@ -80,13 +80,12 @@ impl ServiceConfig { local_addr: Option, ) -> ServiceConfig { let (keep_alive, ka_enabled) = match keep_alive { - 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.then(|| keep_alive); + let keep_alive = (ka_enabled && keep_alive > Duration::ZERO).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 ff60623ae..13004ba2d 100644 --- a/actix-http/src/h1/dispatcher.rs +++ b/actix-http/src/h1/dispatcher.rs @@ -207,6 +207,12 @@ impl TimerState { *self = Self::Inactive; } + + fn init(&mut self, cx: &mut Context<'_>) { + if let TimerState::Active { timer } = self { + let _ = timer.as_mut().poll(cx); + } + } } impl fmt::Display for TimerState { @@ -770,6 +776,8 @@ where } /// Process one incoming request. + /// + /// Boolean in return type indicates if any meaningful work was done. fn poll_request( mut self: Pin<&mut Self>, cx: &mut Context<'_>, @@ -786,11 +794,15 @@ where let mut this = self.as_mut().project(); + let mut updated = false; + loop { log::trace!("attempt to decode frame"); match this.codec.decode(this.read_buf) { Ok(Some(msg)) => { + updated = true; + log::trace!("found full frame (head)"); match msg { @@ -919,8 +931,7 @@ where } } - // TODO: what's this boolean do now? - Ok(false) + Ok(updated) } fn poll_head_timer( @@ -1241,10 +1252,7 @@ where // poll response to populate write buffer // drain indicates whether write buffer should be emptied before next run let drain = match inner.as_mut().poll_response(cx)? { - PollResponse::DrainWriteBuf => { - inner.flags.contains(Flags::KEEP_ALIVE); - true - } + PollResponse::DrainWriteBuf => true, PollResponse::DoNothing => { if inner.flags.contains(Flags::KEEP_ALIVE) { @@ -1255,6 +1263,8 @@ where .project() .ka_timer .set(deadline, line!()); + + inner.as_mut().project().ka_timer.init(cx); } } @@ -1316,6 +1326,7 @@ where "start shutdown because keep-alive is disabled or opted \ out for this connection" ); + inner_p.flags.remove(Flags::FINISHED); inner_p.flags.insert(Flags::SHUTDOWN); return self.poll(cx); } diff --git a/actix-http/src/h1/dispatcher_tests.rs b/actix-http/src/h1/dispatcher_tests.rs index 3a7bec6e0..2fc306290 100644 --- a/actix-http/src/h1/dispatcher_tests.rs +++ b/actix-http/src/h1/dispatcher_tests.rs @@ -71,8 +71,6 @@ fn echo_payload_service() -> impl Service, E #[actix_rt::test] async fn late_request() { - let _ = env_logger::try_init(); - let mut buf = TestBuffer::empty(); let cfg = ServiceConfig::new(KeepAlive::Disabled, 100, 0, false, None); diff --git a/actix-http/src/responses/head.rs b/actix-http/src/responses/head.rs index f0623a846..1525321f9 100644 --- a/actix-http/src/responses/head.rs +++ b/actix-http/src/responses/head.rs @@ -217,8 +217,6 @@ mod tests { #[actix_rt::test] async fn camel_case_headers() { - let _ = env_logger::try_init(); - let mut srv = actix_http_test::test_server(|| { H1Service::with_config(ServiceConfig::default(), |req: Request| async move { let mut res = Response::ok(); diff --git a/actix-http/tests/test_server.rs b/actix-http/tests/test_server.rs index a6b5de470..4ea96419e 100644 --- a/actix-http/tests/test_server.rs +++ b/actix-http/tests/test_server.rs @@ -309,6 +309,8 @@ async fn test_http1_keepalive() { #[actix_rt::test] async fn test_http1_keepalive_timeout() { + let _ = env_logger::try_init(); + let mut srv = test_server(|| { HttpService::build() .keep_alive(1) @@ -317,14 +319,21 @@ async fn test_http1_keepalive_timeout() { }) .await; + log::debug!(target: "actix-test", "connect"); let mut stream = net::TcpStream::connect(srv.addr()).unwrap(); - let _ = stream.write_all(b"GET /test/tests/test HTTP/1.1\r\n\r\n"); - let mut data = vec![0; 1024]; + + log::debug!(target: "actix-test", "send req"); + let _ = stream.write_all(b"GET /test HTTP/1.1\r\n\r\n"); + let mut data = vec![0; 256]; + log::debug!(target: "actix-test", "first read"); let _ = stream.read(&mut data); assert_eq!(&data[..17], b"HTTP/1.1 200 OK\r\n"); + + log::debug!(target: "actix-test", "sleep"); thread::sleep(Duration::from_millis(1100)); - let mut data = vec![0; 1024]; + log::debug!(target: "actix-test", "second read"); + let mut data = vec![0; 256]; let res = stream.read(&mut data).unwrap(); assert_eq!(res, 0);