Introduced the "keepalive_time" directive.
Similar to lingering_time, it limits total connection lifetime before keepalive is switched off. The default is 1 hour, which is close to the total maximum connection lifetime possible with default keepalive_requests and keepalive_timeout.
This commit is contained in:
parent
ef20419dff
commit
abd21d3202
10 changed files with 46 additions and 1 deletions
|
@ -162,6 +162,7 @@ struct ngx_connection_s {
|
||||||
|
|
||||||
ngx_atomic_uint_t number;
|
ngx_atomic_uint_t number;
|
||||||
|
|
||||||
|
ngx_msec_t start_time;
|
||||||
ngx_uint_t requests;
|
ngx_uint_t requests;
|
||||||
|
|
||||||
unsigned buffered:8;
|
unsigned buffered:8;
|
||||||
|
|
|
@ -4459,6 +4459,8 @@ ngx_udp_connect(ngx_resolver_connection_t *rec)
|
||||||
|
|
||||||
c->number = ngx_atomic_fetch_add(ngx_connection_counter, 1);
|
c->number = ngx_atomic_fetch_add(ngx_connection_counter, 1);
|
||||||
|
|
||||||
|
c->start_time = ngx_current_msec;
|
||||||
|
|
||||||
ngx_log_debug3(NGX_LOG_DEBUG_EVENT, &rec->log, 0,
|
ngx_log_debug3(NGX_LOG_DEBUG_EVENT, &rec->log, 0,
|
||||||
"connect to %V, fd:%d #%uA", &rec->server, s, c->number);
|
"connect to %V, fd:%d #%uA", &rec->server, s, c->number);
|
||||||
|
|
||||||
|
@ -4545,6 +4547,8 @@ ngx_tcp_connect(ngx_resolver_connection_t *rec)
|
||||||
|
|
||||||
c->number = ngx_atomic_fetch_add(ngx_connection_counter, 1);
|
c->number = ngx_atomic_fetch_add(ngx_connection_counter, 1);
|
||||||
|
|
||||||
|
c->start_time = ngx_current_msec;
|
||||||
|
|
||||||
if (ngx_add_conn) {
|
if (ngx_add_conn) {
|
||||||
if (ngx_add_conn(c) == NGX_ERROR) {
|
if (ngx_add_conn(c) == NGX_ERROR) {
|
||||||
goto failed;
|
goto failed;
|
||||||
|
|
|
@ -256,6 +256,8 @@ ngx_event_accept(ngx_event_t *ev)
|
||||||
|
|
||||||
c->number = ngx_atomic_fetch_add(ngx_connection_counter, 1);
|
c->number = ngx_atomic_fetch_add(ngx_connection_counter, 1);
|
||||||
|
|
||||||
|
c->start_time = ngx_current_msec;
|
||||||
|
|
||||||
#if (NGX_STAT_STUB)
|
#if (NGX_STAT_STUB)
|
||||||
(void) ngx_atomic_fetch_add(ngx_stat_handled, 1);
|
(void) ngx_atomic_fetch_add(ngx_stat_handled, 1);
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -80,6 +80,8 @@ ngx_event_acceptex(ngx_event_t *rev)
|
||||||
|
|
||||||
c->number = ngx_atomic_fetch_add(ngx_connection_counter, 1);
|
c->number = ngx_atomic_fetch_add(ngx_connection_counter, 1);
|
||||||
|
|
||||||
|
c->start_time = ngx_current_msec;
|
||||||
|
|
||||||
ls->handler(c);
|
ls->handler(c);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -193,6 +193,8 @@ ngx_event_connect_peer(ngx_peer_connection_t *pc)
|
||||||
|
|
||||||
c->number = ngx_atomic_fetch_add(ngx_connection_counter, 1);
|
c->number = ngx_atomic_fetch_add(ngx_connection_counter, 1);
|
||||||
|
|
||||||
|
c->start_time = ngx_current_msec;
|
||||||
|
|
||||||
if (ngx_add_conn) {
|
if (ngx_add_conn) {
|
||||||
if (ngx_add_conn(c) == NGX_ERROR) {
|
if (ngx_add_conn(c) == NGX_ERROR) {
|
||||||
goto failed;
|
goto failed;
|
||||||
|
|
|
@ -363,6 +363,8 @@ ngx_event_recvmsg(ngx_event_t *ev)
|
||||||
|
|
||||||
c->number = ngx_atomic_fetch_add(ngx_connection_counter, 1);
|
c->number = ngx_atomic_fetch_add(ngx_connection_counter, 1);
|
||||||
|
|
||||||
|
c->start_time = ngx_current_msec;
|
||||||
|
|
||||||
#if (NGX_STAT_STUB)
|
#if (NGX_STAT_STUB)
|
||||||
(void) ngx_atomic_fetch_add(ngx_stat_handled, 1);
|
(void) ngx_atomic_fetch_add(ngx_stat_handled, 1);
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -13,6 +13,7 @@
|
||||||
typedef struct {
|
typedef struct {
|
||||||
ngx_uint_t max_cached;
|
ngx_uint_t max_cached;
|
||||||
ngx_uint_t requests;
|
ngx_uint_t requests;
|
||||||
|
ngx_msec_t time;
|
||||||
ngx_msec_t timeout;
|
ngx_msec_t timeout;
|
||||||
|
|
||||||
ngx_queue_t cache;
|
ngx_queue_t cache;
|
||||||
|
@ -86,6 +87,13 @@ static ngx_command_t ngx_http_upstream_keepalive_commands[] = {
|
||||||
0,
|
0,
|
||||||
NULL },
|
NULL },
|
||||||
|
|
||||||
|
{ ngx_string("keepalive_time"),
|
||||||
|
NGX_HTTP_UPS_CONF|NGX_CONF_TAKE1,
|
||||||
|
ngx_conf_set_msec_slot,
|
||||||
|
NGX_HTTP_SRV_CONF_OFFSET,
|
||||||
|
offsetof(ngx_http_upstream_keepalive_srv_conf_t, time),
|
||||||
|
NULL },
|
||||||
|
|
||||||
{ ngx_string("keepalive_timeout"),
|
{ ngx_string("keepalive_timeout"),
|
||||||
NGX_HTTP_UPS_CONF|NGX_CONF_TAKE1,
|
NGX_HTTP_UPS_CONF|NGX_CONF_TAKE1,
|
||||||
ngx_conf_set_msec_slot,
|
ngx_conf_set_msec_slot,
|
||||||
|
@ -149,6 +157,7 @@ ngx_http_upstream_init_keepalive(ngx_conf_t *cf,
|
||||||
kcf = ngx_http_conf_upstream_srv_conf(us,
|
kcf = ngx_http_conf_upstream_srv_conf(us,
|
||||||
ngx_http_upstream_keepalive_module);
|
ngx_http_upstream_keepalive_module);
|
||||||
|
|
||||||
|
ngx_conf_init_msec_value(kcf->time, 3600000);
|
||||||
ngx_conf_init_msec_value(kcf->timeout, 60000);
|
ngx_conf_init_msec_value(kcf->timeout, 60000);
|
||||||
ngx_conf_init_uint_value(kcf->requests, 100);
|
ngx_conf_init_uint_value(kcf->requests, 100);
|
||||||
|
|
||||||
|
@ -326,6 +335,10 @@ ngx_http_upstream_free_keepalive_peer(ngx_peer_connection_t *pc, void *data,
|
||||||
goto invalid;
|
goto invalid;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (ngx_current_msec - c->start_time > kp->conf->time) {
|
||||||
|
goto invalid;
|
||||||
|
}
|
||||||
|
|
||||||
if (!u->keepalive) {
|
if (!u->keepalive) {
|
||||||
goto invalid;
|
goto invalid;
|
||||||
}
|
}
|
||||||
|
@ -513,6 +526,7 @@ ngx_http_upstream_keepalive_create_conf(ngx_conf_t *cf)
|
||||||
* conf->max_cached = 0;
|
* conf->max_cached = 0;
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
conf->time = NGX_CONF_UNSET_MSEC;
|
||||||
conf->timeout = NGX_CONF_UNSET_MSEC;
|
conf->timeout = NGX_CONF_UNSET_MSEC;
|
||||||
conf->requests = NGX_CONF_UNSET_UINT;
|
conf->requests = NGX_CONF_UNSET_UINT;
|
||||||
|
|
||||||
|
|
|
@ -495,6 +495,13 @@ static ngx_command_t ngx_http_core_commands[] = {
|
||||||
offsetof(ngx_http_core_loc_conf_t, limit_rate_after),
|
offsetof(ngx_http_core_loc_conf_t, limit_rate_after),
|
||||||
NULL },
|
NULL },
|
||||||
|
|
||||||
|
{ ngx_string("keepalive_time"),
|
||||||
|
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
||||||
|
ngx_conf_set_msec_slot,
|
||||||
|
NGX_HTTP_LOC_CONF_OFFSET,
|
||||||
|
offsetof(ngx_http_core_loc_conf_t, keepalive_time),
|
||||||
|
NULL },
|
||||||
|
|
||||||
{ ngx_string("keepalive_timeout"),
|
{ ngx_string("keepalive_timeout"),
|
||||||
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE12,
|
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE12,
|
||||||
ngx_http_core_keepalive,
|
ngx_http_core_keepalive,
|
||||||
|
@ -1335,6 +1342,11 @@ ngx_http_update_location_config(ngx_http_request_t *r)
|
||||||
} else if (r->connection->requests >= clcf->keepalive_requests) {
|
} else if (r->connection->requests >= clcf->keepalive_requests) {
|
||||||
r->keepalive = 0;
|
r->keepalive = 0;
|
||||||
|
|
||||||
|
} else if (ngx_current_msec - r->connection->start_time
|
||||||
|
> clcf->keepalive_time)
|
||||||
|
{
|
||||||
|
r->keepalive = 0;
|
||||||
|
|
||||||
} else if (r->headers_in.msie6
|
} else if (r->headers_in.msie6
|
||||||
&& r->method == NGX_HTTP_POST
|
&& r->method == NGX_HTTP_POST
|
||||||
&& (clcf->keepalive_disable
|
&& (clcf->keepalive_disable
|
||||||
|
@ -3500,6 +3512,7 @@ ngx_http_core_create_loc_conf(ngx_conf_t *cf)
|
||||||
clcf->send_timeout = NGX_CONF_UNSET_MSEC;
|
clcf->send_timeout = NGX_CONF_UNSET_MSEC;
|
||||||
clcf->send_lowat = NGX_CONF_UNSET_SIZE;
|
clcf->send_lowat = NGX_CONF_UNSET_SIZE;
|
||||||
clcf->postpone_output = NGX_CONF_UNSET_SIZE;
|
clcf->postpone_output = NGX_CONF_UNSET_SIZE;
|
||||||
|
clcf->keepalive_time = NGX_CONF_UNSET_MSEC;
|
||||||
clcf->keepalive_timeout = NGX_CONF_UNSET_MSEC;
|
clcf->keepalive_timeout = NGX_CONF_UNSET_MSEC;
|
||||||
clcf->keepalive_header = NGX_CONF_UNSET;
|
clcf->keepalive_header = NGX_CONF_UNSET;
|
||||||
clcf->keepalive_requests = NGX_CONF_UNSET_UINT;
|
clcf->keepalive_requests = NGX_CONF_UNSET_UINT;
|
||||||
|
@ -3738,6 +3751,8 @@ ngx_http_core_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
|
||||||
conf->limit_rate_after = prev->limit_rate_after;
|
conf->limit_rate_after = prev->limit_rate_after;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ngx_conf_merge_msec_value(conf->keepalive_time,
|
||||||
|
prev->keepalive_time, 3600000);
|
||||||
ngx_conf_merge_msec_value(conf->keepalive_timeout,
|
ngx_conf_merge_msec_value(conf->keepalive_timeout,
|
||||||
prev->keepalive_timeout, 75000);
|
prev->keepalive_timeout, 75000);
|
||||||
ngx_conf_merge_sec_value(conf->keepalive_header,
|
ngx_conf_merge_sec_value(conf->keepalive_header,
|
||||||
|
|
|
@ -359,6 +359,7 @@ struct ngx_http_core_loc_conf_s {
|
||||||
|
|
||||||
ngx_msec_t client_body_timeout; /* client_body_timeout */
|
ngx_msec_t client_body_timeout; /* client_body_timeout */
|
||||||
ngx_msec_t send_timeout; /* send_timeout */
|
ngx_msec_t send_timeout; /* send_timeout */
|
||||||
|
ngx_msec_t keepalive_time; /* keepalive_time */
|
||||||
ngx_msec_t keepalive_timeout; /* keepalive_timeout */
|
ngx_msec_t keepalive_timeout; /* keepalive_timeout */
|
||||||
ngx_msec_t lingering_time; /* lingering_time */
|
ngx_msec_t lingering_time; /* lingering_time */
|
||||||
ngx_msec_t lingering_timeout; /* lingering_timeout */
|
ngx_msec_t lingering_timeout; /* lingering_timeout */
|
||||||
|
|
|
@ -1369,7 +1369,9 @@ ngx_http_v2_state_headers(ngx_http_v2_connection_t *h2c, u_char *pos,
|
||||||
ngx_http_core_module);
|
ngx_http_core_module);
|
||||||
|
|
||||||
if (clcf->keepalive_timeout == 0
|
if (clcf->keepalive_timeout == 0
|
||||||
|| h2c->connection->requests >= clcf->keepalive_requests)
|
|| h2c->connection->requests >= clcf->keepalive_requests
|
||||||
|
|| ngx_current_msec - h2c->connection->start_time
|
||||||
|
> clcf->keepalive_time)
|
||||||
{
|
{
|
||||||
h2c->goaway = 1;
|
h2c->goaway = 1;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue