Skip to content

Commit

Permalink
Avoid sending "Connection: keep-alive" when shutting down.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mdounin committed Jan 10, 2022
1 parent 96c342e commit 22d4ff0
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/http/ngx_http_header_filter_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,10 @@ ngx_http_header_filter(ngx_http_request_t *r)
}
}

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;
Expand Down

0 comments on commit 22d4ff0

Please sign in to comment.