From 04d7cfeef6205d2cf33ad5704a363ee107250771 Mon Sep 17 00:00:00 2001 From: Jordan Wu <101218661+jordan-definitive@users.noreply.github.com> Date: Wed, 18 Sep 2024 02:08:07 +0000 Subject: shutdown h2 connection gracefully with GOAWAYs Co-authored-by: Matthew Gumport Includes-commit: 15e5c64ce46da5e061f2c84ce45cdddedf2f6058 Replicated-from: https://github.com/cloudflare/pingora/pull/415 --- .bleep | 2 +- pingora-core/src/apps/mod.rs | 13 +++++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/.bleep b/.bleep index 26298b0..0c316c3 100644 --- a/.bleep +++ b/.bleep @@ -1 +1 @@ -78684dca2f7fe5d952d3b01faec526a1b1cbad8c \ No newline at end of file +a4032182c556d6fee5f22d0b56b99b03c8fc6868 \ No newline at end of file diff --git a/pingora-core/src/apps/mod.rs b/pingora-core/src/apps/mod.rs index 0786d08..2a77a3d 100644 --- a/pingora-core/src/apps/mod.rs +++ b/pingora-core/src/apps/mod.rs @@ -20,6 +20,7 @@ pub mod prometheus_http_app; use crate::server::ShutdownWatch; use async_trait::async_trait; use log::{debug, error}; +use std::future::poll_fn; use std::sync::Arc; use crate::protocols::http::v2::server; @@ -148,11 +149,19 @@ where Ok(c) => c, }; + let mut shutdown = shutdown.clone(); loop { // this loop ends when the client decides to close the h2 conn // TODO: add a timeout? - let h2_stream = - server::HttpSession::from_h2_conn(&mut h2_conn, digest.clone()).await; + let h2_stream = tokio::select! { + _ = shutdown.changed() => { + h2_conn.graceful_shutdown(); + let _ = poll_fn(|cx| h2_conn.poll_closed(cx)) + .await.map_err(|e| error!("H2 error waiting for shutdown {e}")); + return None; + } + h2_stream = server::HttpSession::from_h2_conn(&mut h2_conn, digest.clone()) => h2_stream + }; let h2_stream = match h2_stream { Err(e) => { // It is common for the client to just disconnect TCP without properly -- cgit v1.2.3