]> git.kaiwu.me - nginx.git/commitdiff
QUIC: idle mode for main connection.
authorRoman Arutyunyan <arut@nginx.com>
Wed, 19 Oct 2022 13:45:18 +0000 (17:45 +0400)
committerRoman Arutyunyan <arut@nginx.com>
Wed, 19 Oct 2022 13:45:18 +0000 (17:45 +0400)
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 gracefully shuts down
the QUIC connection.

src/event/quic/ngx_event_quic.c
src/event/quic/ngx_event_quic.h
src/http/v3/ngx_http_v3.h
src/http/v3/ngx_http_v3_module.c
src/http/v3/ngx_http_v3_request.c

index 7245ee7d05baef05068b76168e003971611c91d8..a28f5a7ac48209eac7b5dbc7ed083b2a4fdf39bc 100644 (file)
@@ -341,6 +341,7 @@ ngx_quic_new_connection(ngx_connection_t *c, ngx_quic_conf_t *conf,
         return NULL;
     }
 
+    c->idle = 1;
     ngx_reusable_connection(c, 1);
 
     ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, 0,
@@ -420,9 +421,19 @@ 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);
+        c->close = 0;
+
+        if (!ngx_exiting) {
+            qc->error = NGX_QUIC_ERR_NO_ERROR;
+            qc->error_reason = "graceful shutdown";
+            ngx_quic_close_connection(c, NGX_ERROR);
+            return;
+        }
+
+        if (!qc->closing && qc->conf->shutdown) {
+            qc->conf->shutdown(c);
+        }
+
         return;
     }
 
index d33bd8d4da68db951cba91b76bee196efba5d0f8..335d925cb08ea45ae860dd0bbd01954446684a3b 100644 (file)
@@ -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;
index 7ffd38768e8a85cf0f0ba0e2c6f53d3abd8a0634..818ca2bf5182355b21811809dae6034ddaf40326 100644 (file)
@@ -141,6 +141,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;
@@ -158,6 +159,7 @@ void ngx_http_v3_init(ngx_connection_t *c);
 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);
index d274a3bf2368d7d6d29cec8c7269512bcfda5793..d87a1c9efdef7d43e59dd20f5c14d1bcab2015ac 100644 (file)
@@ -249,6 +249,8 @@ ngx_http_v3_create_srv_conf(ngx_conf_t *cf)
     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;
 }
 
index bdb5331fe7da3d6b79025d76513f5996413ad709..c75bc19c5c9db79eab3a32cb237d428e89496d5a 100644 (file)
@@ -100,6 +100,37 @@ 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");
+
+    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;
+
+#if (NGX_HTTP_V3_HQ)
+        if (!h3c->hq)
+#endif
+        {
+            (void) ngx_http_v3_send_goaway(c, h3c->next_request_id);
+        }
+
+        ngx_http_v3_shutdown_connection(c, NGX_HTTP_V3_ERR_NO_ERROR,
+                                        "connection shutdown");
+    }
+}
+
+
 static void
 ngx_http_v3_init_request_stream(ngx_connection_t *c)
 {
@@ -140,6 +171,8 @@ ngx_http_v3_init_request_stream(ngx_connection_t *c)
 
     pc = c->quic->parent;
 
+    h3c->next_request_id = c->quic->id + 0x04;
+
     if (n + 1 == clcf->keepalive_requests
         || ngx_current_msec - pc->start_time > clcf->keepalive_time)
     {
@@ -149,7 +182,7 @@ ngx_http_v3_init_request_stream(ngx_connection_t *c)
         if (!h3c->hq)
 #endif
         {
-            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;
             }