[PATCH 6 of 8] QUIC: idle mode for main connection

Roman Arutyunyan arut at nginx.com
Thu Jun 23 15:58:44 UTC 2022


# HG changeset patch
# User Roman Arutyunyan <arut at nginx.com>
# Date 1655995473 -14400
#      Thu Jun 23 18:44:33 2022 +0400
# Branch quic
# Node ID 1fad6f74168bdceaf3cd59ae72f7c6128dfaa84d
# Parent  56b6cd5bc78bba1085a168c0f5ea9a03d34646c5
QUIC: idle mode for main connection.

Now main QUIC connection for HTTP/3 always has c->idle flag set.  This allows
the connection to receive worker shutdown notification.  It is passed to
application level via a new conf->shutdown() callback.

The HTTP/3 shutdown callback sends GOAWAY to client and enables rejecting
new client request streams.

diff --git a/src/event/quic/ngx_event_quic.c b/src/event/quic/ngx_event_quic.c
--- a/src/event/quic/ngx_event_quic.c
+++ b/src/event/quic/ngx_event_quic.c
@@ -415,9 +415,9 @@ ngx_quic_input_handler(ngx_event_t *rev)
     }
 
     if (c->close) {
-        qc->error = NGX_QUIC_ERR_NO_ERROR;
-        qc->error_reason = "graceful shutdown";
-        ngx_quic_close_connection(c, NGX_ERROR);
+        if (qc->conf->shutdown) {
+            qc->conf->shutdown(c);
+        }
         return;
     }
 
diff --git a/src/event/quic/ngx_event_quic.h b/src/event/quic/ngx_event_quic.h
--- a/src/event/quic/ngx_event_quic.h
+++ b/src/event/quic/ngx_event_quic.h
@@ -28,6 +28,9 @@
 #define NGX_QUIC_STREAM_UNIDIRECTIONAL       0x02
 
 
+typedef void (*ngx_quic_shutdown_pt)(ngx_connection_t *c);
+
+
 typedef enum {
     NGX_QUIC_STREAM_SEND_READY = 0,
     NGX_QUIC_STREAM_SEND_SEND,
@@ -74,6 +77,8 @@ typedef struct {
     ngx_int_t                      stream_reject_code_uni;
     ngx_int_t                      stream_reject_code_bidi;
 
+    ngx_quic_shutdown_pt           shutdown;
+
     u_char                         av_token_key[NGX_QUIC_AV_KEY_LEN];
     u_char                         sr_token_key[NGX_QUIC_SR_KEY_LEN];
 } ngx_quic_conf_t;
diff --git a/src/http/v3/ngx_http_v3.h b/src/http/v3/ngx_http_v3.h
--- a/src/http/v3/ngx_http_v3.h
+++ b/src/http/v3/ngx_http_v3.h
@@ -137,6 +137,7 @@ struct ngx_http_v3_session_s {
     uint64_t                      next_push_id;
     uint64_t                      max_push_id;
     uint64_t                      goaway_push_id;
+    uint64_t                      next_request_id;
 
     off_t                         total_bytes;
     off_t                         payload_bytes;
@@ -151,6 +152,7 @@ void ngx_http_v3_init(ngx_connection_t *
 void ngx_http_v3_reset_connection(ngx_connection_t *c);
 ngx_int_t ngx_http_v3_init_session(ngx_connection_t *c);
 ngx_int_t ngx_http_v3_check_flood(ngx_connection_t *c);
+void ngx_http_v3_shutdown(ngx_connection_t *c);
 
 ngx_int_t ngx_http_v3_read_request_body(ngx_http_request_t *r);
 ngx_int_t ngx_http_v3_read_unbuffered_request_body(ngx_http_request_t *r);
diff --git a/src/http/v3/ngx_http_v3_module.c b/src/http/v3/ngx_http_v3_module.c
--- a/src/http/v3/ngx_http_v3_module.c
+++ b/src/http/v3/ngx_http_v3_module.c
@@ -249,6 +249,8 @@ ngx_http_v3_create_srv_conf(ngx_conf_t *
     h3scf->quic.stream_reject_code_bidi = NGX_HTTP_V3_ERR_REQUEST_REJECTED;
     h3scf->quic.active_connection_id_limit = NGX_CONF_UNSET_UINT;
 
+    h3scf->quic.shutdown = ngx_http_v3_shutdown;
+
     return h3scf;
 }
 
diff --git a/src/http/v3/ngx_http_v3_request.c b/src/http/v3/ngx_http_v3_request.c
--- a/src/http/v3/ngx_http_v3_request.c
+++ b/src/http/v3/ngx_http_v3_request.c
@@ -74,6 +74,7 @@ ngx_http_v3_init(ngx_connection_t *c)
     h3scf = ngx_http_get_module_srv_conf(hc->conf_ctx, ngx_http_v3_module);
 
     if (c->quic == NULL) {
+        c->idle = 1;
         h3scf->quic.timeout = clcf->keepalive_timeout;
         ngx_quic_run(c, &h3scf->quic);
         return;
@@ -116,6 +117,40 @@ ngx_http_v3_init(ngx_connection_t *c)
 }
 
 
+void
+ngx_http_v3_shutdown(ngx_connection_t *c)
+{
+    ngx_http_v3_session_t   *h3c;
+
+    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http3 shutdown");
+
+    if (c->reusable) {
+        ngx_quic_terminate_connection(c, NGX_HTTP_V3_ERR_NO_ERROR,
+                                      "connection shutdown");
+        return;
+    }
+
+    h3c = ngx_http_v3_get_session(c);
+
+    if (h3c == NULL) {
+        ngx_quic_finalize_connection(c, NGX_HTTP_V3_ERR_NO_ERROR,
+                                     "connection shutdown");
+        return;
+    }
+
+    if (!h3c->goaway) {
+        h3c->goaway = 1;
+
+        (void) ngx_http_v3_send_goaway(c, h3c->next_request_id);
+    }
+
+    if (h3c->nrequests == 0) {
+        ngx_quic_finalize_connection(c, NGX_HTTP_V3_ERR_NO_ERROR,
+                                     "connection shutdown");
+    }
+}
+
+
 #if (NGX_HTTP_V3_HQ)
 
 static void
@@ -246,17 +281,14 @@ ngx_http_v3_init_request_stream(ngx_conn
 
     h3c = ngx_http_v3_get_session(c);
 
-    if (!h3c->goaway && (ngx_terminate || ngx_exiting)) {
-        h3c->goaway = 1;
-        (void) ngx_http_v3_send_goaway(c, c->quic->id);
-    }
-
     if (h3c->goaway) {
         c->close = 1;
         ngx_http_close_connection(c);
         return;
     }
 
+    h3c->next_request_id = c->quic->id + 0x04;
+
     pc = c->quic->parent;
 
     if (n + 1 == clcf->keepalive_requests
@@ -264,7 +296,7 @@ ngx_http_v3_init_request_stream(ngx_conn
     {
         h3c->goaway = 1;
 
-        if (ngx_http_v3_send_goaway(c, (n + 1) << 2) != NGX_OK) {
+        if (ngx_http_v3_send_goaway(c, h3c->next_request_id) != NGX_OK) {
             ngx_http_close_connection(c);
             return;
         }



More information about the nginx-devel mailing list