8000 HTTP/2: lingering close after GOAWAY. · nginx/nginx@829c9d5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 829c9d5

Browse files
committed
HTTP/2: lingering close after GOAWAY.
After sending the GOAWAY frame, a connection is now closed using the lingering close mechanism. This allows for the reliable delivery of the GOAWAY frames, while also fixing connection resets observed when http2_max_requests is reached (ticket #1250), or with graceful shutdown (ticket #1544), when some additional data from the client is received on a fully closed connection. For HTTP/2, the settings lingering_close, lingering_timeout, and lingering_time are taken from the "server" level.
1 parent fa2f2e3 commit 829c9d5

File tree

2 files changed

+124
-6
lines changed

2 files changed

+124
-6
lines changed

src/http/v2/ngx_http_v2.c

Lines changed: 122 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ typedef struct {
6060
static void ngx_http_v2_read_handler(ngx_event_t *rev);
6161
static void ngx_http_v2_write_handler(ngx_event_t *wev);
6262
static void ngx_http_v2_handle_connection(ngx_http_v2_connection_t *h2c);
63+
static void ngx_http_v2_lingering_close(ngx_http_v2_connection_t *h2c);
64+
static void ngx_http_v2_lingering_close_handler(ngx_event_t *rev);
6365

6466
static u_char *ngx_http_v2_state_proxy_protocol(ngx_http_v2_connection_t *h2c,
6567
u_char *pos, u_char *end);
@@ -661,7 +663,7 @@ ngx_http_v2_handle_connection(ngx_http_v2_connection_t *h2c)
661663
}
662664

663665
if (h2c->goaway) {
664-
ngx_http_close_connection(c);
666+
ngx_http_v2_lingering_close(h2c);
665667
return;
666668
}
667669

@@ -699,6 +701,113 @@ ngx_http_v2_handle_connection(ngx_http_v2_connection_t *h2c)
699701
}
700702

701703

704+
static void
705+
ngx_http_v2_lingering_close(ngx_http_v2_connection_t *h2c)
706+
{
707+
ngx_event_t *rev, *wev;
708+
ngx_connection_t *c;
709+
ngx_http_core_loc_conf_t *clcf;
710+
711+
c = h2c->connection;
712+
713+
clcf = ngx_http_get_module_loc_conf(h2c->http_connection->conf_ctx,
714+
ngx_http_core_module);
715+
716+
if (clcf->lingering_close == NGX_HTTP_LINGERING_OFF) {
717+
ngx_http_close_connection(c);
718+
return;
719+
}
720+
721+
rev = c->read;
722+
rev->handler = ngx_http_v2_lingering_close_handler;
723+
724+
h2c->lingering_time = ngx_time() + (time_t) (clcf->lingering_time / 1000);
725+
ngx_add_timer(rev, clcf->lingering_timeout);
726+
727+
if (ngx_handle_read_event(rev, 0) != NGX_OK) {
728+
ngx_http_close_connection(c);
729+
return;
730+
}
731+
732+
wev = c->write;
733+
wev->handler = ngx_http_empty_handler;
734+
735+
if (wev->active && (ngx_event_flags & NGX_USE_LEVEL_EVENT)) {
736+
if (ngx_del_event(wev, NGX_WRITE_EVENT, 0) != NGX_OK) {
737+
ngx_http_close_connection(c);
738+
return;
739+
}
740+
}
741+
742+
if (ngx_shutdown_socket(c->fd, NGX_WRITE_SHUTDOWN) == -1) {
743+
ngx_connection_error(c, ngx_socket_errno,
744+
ngx_shutdown_socket_n " failed");
745+
ngx_http_close_connection(c);
746+
return;
747+
}
748+
749+
if (rev->ready) {
750+
ngx_http_v2_lingering_close_handler(rev);
751+
}
752+
}
753+
754+
755+
static void
756+
ngx_http_v2_lingering_close_handler(ngx_event_t *rev)
757+
{
758+
ssize_t n;
759+
ngx_msec_t timer;
760+
ngx_connection_t *c;
761+
ngx_http_core_loc_conf_t *clcf;
762+
ngx_http_v2_connection_t *h2c;
763+
u_char buffer[NGX_HTTP_LINGERING_BUFFER_SIZE];
764+
765+
c = rev->data;
766+
h2c = c->data;
767+
768+
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
769+
"http2 lingering close handler");
770+
771+
if (rev->timedout) {
772+
ngx_http_close_connection(c);
773+
return;
774+
}
775+
776+
timer = (ngx_msec_t) h2c->lingering_time - (ngx_msec_t) ngx_time();
777+
if ((ngx_msec_int_t) timer <= 0) {
778+
ngx_http_close_connection(c);
779+
return;
780+
}
781+
782+
do {
783+
n = c->recv(c, buffer, NGX_HTTP_LINGERING_BUFFER_SIZE);
784+
785+
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "lingering read: %z", n);
786+
787+
if (n == NGX_ERROR || n == 0) {
788+
ngx_http_close_connection(c);
789+
return;
790+
}
791+
792+
} while (rev->ready);
793+
794+
if (ngx_handle_read_event(rev, 0) != NGX_OK) {
795+
ngx_http_close_connection(c);
796+
return;
797+
}
798+
799+
clcf = ngx_http_get_module_loc_conf(h2c->http_connection->conf_ctx,
800+
ngx_http_core_module);
801+
timer *= 1000;
802+
803+
if (timer > clcf->lingering_timeout) {
804+
timer = clcf->lingering_timeout;
805+
}
806+
807+
ngx_add_timer(rev, timer);
808+
}
809+
810+
702811
static u_char *
703812
ngx_http_v2_state_proxy_protocol(ngx_http_v2_connection_t *h2c, u_char *pos,
704813
u_char *end)
@@ -4541,16 +4650,15 @@ ngx_http_v2_finalize_connection(ngx_http_v2_connection_t *h2c,
45414650
h2c->blocked = 1;
45424651

45434652
if (!c->error && !h2c->goaway) {
4653+
h2c->goaway = 1;
4654+
45444655
if (ngx_http_v2_send_goaway(h2c, status) != NGX_ERROR) {
45454656
(void) ngx_http_v2_send_output_queue(h2c);
45464657
}
45474658
}
45484659

4549-
c->error = 1;
4550-
45514660
if (!h2c->processing && !h2c->pushing) {
4552-
ngx_http_close_connection(c);
4553-
return;
4661+
goto done;
45544662
}
45554663

45564664
c->read->handler = ngx_http_empty_handler;
@@ -4598,10 +4706,18 @@ ngx_http_v2_finalize_connection(ngx_http_v2_connection_t *h2c,
45984706
h2c->blocked = 0;
45994707

46004708
if (h2c->processing || h2c->pushing) {
4709+
c->error = 1;
4710+
return;
4711+
}
4712+
4713+
done:
4714+
4715+
if (c->error) {
4716+
ngx_http_close_connection(c);
46014717
return;
46024718
}
46034719

4604-
ngx_http_close_connection(c);
4720+
ngx_http_v2_lingering_close(h2c);
46054721
}
46064722

46074723

src/http/v2/ngx_http_v2.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ struct ngx_http_v2_connection_s {
157157
ngx_uint_t last_sid;
158158
ngx_uint_t last_push;
159159

160+
time_t lingering_time;
161+
160162
unsigned closed_nodes:8;
161163
unsigned settings_ack:1;
162164
unsigned table_update:1;

0 commit comments

Comments
 (0)
0