Welcome! Log In Create A New Profile

Advanced

[nginx] Avoid sending "Connection: keep-alive" when shutting down.

Maxim Dounin
January 10, 2022 08:14PM
details: https://hg.nginx.org/nginx/rev/96ae8e57b3dd
branches:
changeset: 7993:96ae8e57b3dd
user: Maxim Dounin <mdounin@mdounin.ru>
date: Tue Jan 11 02:23:49 2022 +0300
description:
Avoid sending "Connection: keep-alive" when shutting down.

When a worker process is shutting down, keepalive is not used: this is checked
before the ngx_http_set_keepalive() call in ngx_http_finalize_connection().
Yet the "Connection: keep-alive" header was still sent, even if we know that
the worker process is shutting down, potentially resulting in additional
requests being sent to the connection which is going to be closed anyway.
While clients are expected to be able to handle asynchronous close events
(see ticket #1022), it is certainly possible to send the "Connection: close"
header instead, informing the client that the connection is going to be closed
and potentially saving some unneeded work.

With this change, we additionally check for worker process shutdown just
before sending response headers, and disable keepalive accordingly.

diffstat:

src/http/ngx_http_header_filter_module.c | 4 ++++
1 files changed, 4 insertions(+), 0 deletions(-)

diffs (14 lines):

diff -r e2d07e4ec636 -r 96ae8e57b3dd src/http/ngx_http_header_filter_module.c
--- a/src/http/ngx_http_header_filter_module.c Thu Dec 30 01:08:46 2021 +0300
+++ b/src/http/ngx_http_header_filter_module.c Tue Jan 11 02:23:49 2022 +0300
@@ -197,6 +197,10 @@ ngx_http_header_filter(ngx_http_request_
}
}

+ if (r->keepalive && (ngx_terminate || ngx_exiting)) {
+ r->keepalive = 0;
+ }
+
len = sizeof("HTTP/1.x ") - 1 + sizeof(CRLF) - 1
/* the end of the header */
+ sizeof(CRLF) - 1;
_______________________________________________
nginx-devel mailing list
nginx-devel@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx-devel
Subject Author Views Posted

[nginx] Avoid sending "Connection: keep-alive" when shutting down.

Maxim Dounin 117 January 10, 2022 08:14PM



Sorry, you do not have permission to post/reply in this forum.

Online Users

Guests: 47
Record Number of Users: 6 on February 13, 2018
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready