comparison src/http/modules/ngx_http_upstream_keepalive_module.c @ 7340:70c6b08973a0

Upstream keepalive: keepalive_requests directive. The directive configures maximum number of requests allowed on a connection kept in the cache. Once a connection reaches the number of requests configured, it is no longer saved to the cache. The default is 100. Much like keepalive_requests for client connections, this is mostly a safeguard to make sure connections are closed periodically and the memory allocated from the connection pool is freed.
author Maxim Dounin <mdounin@mdounin.ru>
date Fri, 10 Aug 2018 21:54:46 +0300
parents d9029e113a05
children 3939483cd1b5
comparison
equal deleted inserted replaced
7339:d9029e113a05 7340:70c6b08973a0
10 #include <ngx_http.h> 10 #include <ngx_http.h>
11 11
12 12
13 typedef struct { 13 typedef struct {
14 ngx_uint_t max_cached; 14 ngx_uint_t max_cached;
15 ngx_uint_t requests;
15 ngx_msec_t timeout; 16 ngx_msec_t timeout;
16 17
17 ngx_queue_t cache; 18 ngx_queue_t cache;
18 ngx_queue_t free; 19 ngx_queue_t free;
19 20
88 { ngx_string("keepalive_timeout"), 89 { ngx_string("keepalive_timeout"),
89 NGX_HTTP_UPS_CONF|NGX_CONF_TAKE1, 90 NGX_HTTP_UPS_CONF|NGX_CONF_TAKE1,
90 ngx_conf_set_msec_slot, 91 ngx_conf_set_msec_slot,
91 NGX_HTTP_SRV_CONF_OFFSET, 92 NGX_HTTP_SRV_CONF_OFFSET,
92 offsetof(ngx_http_upstream_keepalive_srv_conf_t, timeout), 93 offsetof(ngx_http_upstream_keepalive_srv_conf_t, timeout),
94 NULL },
95
96 { ngx_string("keepalive_requests"),
97 NGX_HTTP_UPS_CONF|NGX_CONF_TAKE1,
98 ngx_conf_set_num_slot,
99 NGX_HTTP_SRV_CONF_OFFSET,
100 offsetof(ngx_http_upstream_keepalive_srv_conf_t, requests),
93 NULL }, 101 NULL },
94 102
95 ngx_null_command 103 ngx_null_command
96 }; 104 };
97 105
140 148
141 kcf = ngx_http_conf_upstream_srv_conf(us, 149 kcf = ngx_http_conf_upstream_srv_conf(us,
142 ngx_http_upstream_keepalive_module); 150 ngx_http_upstream_keepalive_module);
143 151
144 ngx_conf_init_msec_value(kcf->timeout, 60000); 152 ngx_conf_init_msec_value(kcf->timeout, 60000);
153 ngx_conf_init_uint_value(kcf->requests, 100);
145 154
146 if (kcf->original_init_upstream(cf, us) != NGX_OK) { 155 if (kcf->original_init_upstream(cf, us) != NGX_OK) {
147 return NGX_ERROR; 156 return NGX_ERROR;
148 } 157 }
149 158
310 || c->write->timedout) 319 || c->write->timedout)
311 { 320 {
312 goto invalid; 321 goto invalid;
313 } 322 }
314 323
324 if (c->requests >= kp->conf->requests) {
325 goto invalid;
326 }
327
315 if (!u->keepalive) { 328 if (!u->keepalive) {
316 goto invalid; 329 goto invalid;
317 } 330 }
318 331
319 if (!u->request_body_sent) { 332 if (!u->request_body_sent) {
498 * conf->original_init_peer = NULL; 511 * conf->original_init_peer = NULL;
499 * conf->max_cached = 0; 512 * conf->max_cached = 0;
500 */ 513 */
501 514
502 conf->timeout = NGX_CONF_UNSET_MSEC; 515 conf->timeout = NGX_CONF_UNSET_MSEC;
516 conf->requests = NGX_CONF_UNSET_UINT;
503 517
504 return conf; 518 return conf;
505 } 519 }
506 520
507 521