Klaus Demo nginx / 0e26538
Variables support in limit_rate and limit_rate_after (ticket #293). Ruslan Ermilov 3 years ago
6 changed file(s) with 57 addition(s) and 44 deletion(s). Raw diff Collapse all Expand all
478478 { ngx_string("limit_rate"),
479479 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
480480 |NGX_CONF_TAKE1,
481 ngx_conf_set_size_slot,
481 ngx_http_set_complex_value_size_slot,
482482 NGX_HTTP_LOC_CONF_OFFSET,
483483 offsetof(ngx_http_core_loc_conf_t, limit_rate),
484484 NULL },
486486 { ngx_string("limit_rate_after"),
487487 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
488488 |NGX_CONF_TAKE1,
489 ngx_conf_set_size_slot,
489 ngx_http_set_complex_value_size_slot,
490490 NGX_HTTP_LOC_CONF_OFFSET,
491491 offsetof(ngx_http_core_loc_conf_t, limit_rate_after),
492492 NULL },
12781278 if (!clcf->tcp_nopush) {
12791279 /* disable TCP_NOPUSH/TCP_CORK use */
12801280 r->connection->tcp_nopush = NGX_TCP_NOPUSH_DISABLED;
1281 }
1282
1283 if (r->limit_rate == 0) {
1284 r->limit_rate = clcf->limit_rate;
12851281 }
12861282
12871283 if (clcf->handler) {
33863382 * clcf->exact_match = 0;
33873383 * clcf->auto_redirect = 0;
33883384 * clcf->alias = 0;
3385 * clcf->limit_rate = NULL;
3386 * clcf->limit_rate_after = NULL;
33893387 * clcf->gzip_proxied = 0;
33903388 * clcf->keepalive_disable = 0;
33913389 */
34163414 clcf->send_timeout = NGX_CONF_UNSET_MSEC;
34173415 clcf->send_lowat = NGX_CONF_UNSET_SIZE;
34183416 clcf->postpone_output = NGX_CONF_UNSET_SIZE;
3419 clcf->limit_rate = NGX_CONF_UNSET_SIZE;
3420 clcf->limit_rate_after = NGX_CONF_UNSET_SIZE;
34213417 clcf->keepalive_timeout = NGX_CONF_UNSET_MSEC;
34223418 clcf->keepalive_header = NGX_CONF_UNSET;
34233419 clcf->keepalive_requests = NGX_CONF_UNSET_UINT;
36463642 ngx_conf_merge_size_value(conf->send_lowat, prev->send_lowat, 0);
36473643 ngx_conf_merge_size_value(conf->postpone_output, prev->postpone_output,
36483644 1460);
3649 ngx_conf_merge_size_value(conf->limit_rate, prev->limit_rate, 0);
3650 ngx_conf_merge_size_value(conf->limit_rate_after, prev->limit_rate_after,
3651 0);
3645
3646 if (conf->limit_rate == NULL) {
3647 conf->limit_rate = prev->limit_rate;
3648 }
3649
3650 if (conf->limit_rate_after == NULL) {
3651 conf->limit_rate_after = prev->limit_rate_after;
3652 }
3653
36523654 ngx_conf_merge_msec_value(conf->keepalive_timeout,
36533655 prev->keepalive_timeout, 75000);
36543656 ngx_conf_merge_sec_value(conf->keepalive_header,
348348 size_t client_body_buffer_size; /* client_body_buffer_size */
349349 size_t send_lowat; /* send_lowat */
350350 size_t postpone_output; /* postpone_output */
351 size_t limit_rate; /* limit_rate */
352 size_t limit_rate_after; /* limit_rate_after */
353351 size_t sendfile_max_chunk; /* sendfile_max_chunk */
354352 size_t read_ahead; /* read_ahead */
355353 size_t subrequest_output_buffer_size;
356354 /* subrequest_output_buffer_size */
355
356 ngx_http_complex_value_t *limit_rate; /* limit_rate */
357 ngx_http_complex_value_t *limit_rate_after; /* limit_rate_after */
357358
358359 ngx_msec_t client_body_timeout; /* client_body_timeout */
359360 ngx_msec_t send_timeout; /* send_timeout */
513513 */
514514 unsigned limit_conn_set:1;
515515 unsigned limit_req_set:1;
516
517 unsigned limit_rate_set:1;
518 unsigned limit_rate_after_set:1;
516519
517520 #if 0
518521 unsigned cacheable:1;
29782978 ngx_http_upstream_process_non_buffered_downstream;
29792979
29802980 r->limit_rate = 0;
2981 r->limit_rate_set = 1;
29812982
29822983 if (u->input_filter_init(u->input_filter_ctx) == NGX_ERROR) {
29832984 ngx_http_upstream_finalize_request(r, u, NGX_ERROR);
48054806
48064807 if (n != NGX_ERROR) {
48074808 r->limit_rate = (size_t) n;
4809 r->limit_rate_set = 1;
48084810 }
48094811
48104812 return NGX_OK;
2121 #endif
2222 static ngx_int_t ngx_http_variable_request_get_size(ngx_http_request_t *r,
2323 ngx_http_variable_value_t *v, uintptr_t data);
24 static void ngx_http_variable_request_set_size(ngx_http_request_t *r,
25 ngx_http_variable_value_t *v, uintptr_t data);
2624 static ngx_int_t ngx_http_variable_header(ngx_http_request_t *r,
2725 ngx_http_variable_value_t *v, uintptr_t data);
2826
122120 static ngx_int_t ngx_http_variable_sent_keep_alive(ngx_http_request_t *r,
123121 ngx_http_variable_value_t *v, uintptr_t data);
124122 static ngx_int_t ngx_http_variable_sent_transfer_encoding(ngx_http_request_t *r,
123 ngx_http_variable_value_t *v, uintptr_t data);
124 static void ngx_http_variable_set_limit_rate(ngx_http_request_t *r,
125125 ngx_http_variable_value_t *v, uintptr_t data);
126126
127127 static ngx_int_t ngx_http_variable_connection(ngx_http_request_t *r,
320320 { ngx_string("sent_http_link"), NULL, ngx_http_variable_headers,
321321 offsetof(ngx_http_request_t, headers_out.link), 0, 0 },
322322
323 { ngx_string("limit_rate"), ngx_http_variable_request_set_size,
323 { ngx_string("limit_rate"), ngx_http_variable_set_limit_rate,
324324 ngx_http_variable_request_get_size,
325325 offsetof(ngx_http_request_t, limit_rate),
326326 NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE, 0 },
787787 }
788788
789789
790 static void
791 ngx_http_variable_request_set_size(ngx_http_request_t *r,
792 ngx_http_variable_value_t *v, uintptr_t data)
793 {
794 ssize_t s, *sp;
795 ngx_str_t val;
796
797 val.len = v->len;
798 val.data = v->data;
799
800 s = ngx_parse_size(&val);
801
802 if (s == NGX_ERROR) {
803 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
804 "invalid size \"%V\"", &val);
805 return;
806 }
807
808 sp = (ssize_t *) ((char *) r + data);
809
810 *sp = s;
811
812 return;
813 }
814
815
816790 static ngx_int_t
817791 ngx_http_variable_header(ngx_http_request_t *r, ngx_http_variable_value_t *v,
818792 uintptr_t data)
19891963 }
19901964
19911965 return NGX_OK;
1966 }
1967
1968
1969 static void
1970 ngx_http_variable_set_limit_rate(ngx_http_request_t *r,
1971 ngx_http_variable_value_t *v, uintptr_t data)
1972 {
1973 ssize_t s;
1974 ngx_str_t val;
1975
1976 val.len = v->len;
1977 val.data = v->data;
1978
1979 s = ngx_parse_size(&val);
1980
1981 if (s == NGX_ERROR) {
1982 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1983 "invalid $limit_rate \"%V\"", &val);
1984 return;
1985 }
1986
1987 r->limit_rate = s;
1988 r->limit_rate_set = 1;
19921989 }
19931990
19941991
249249 return NGX_ERROR;
250250 }
251251
252 if (!r->limit_rate_set) {
253 r->limit_rate = ngx_http_complex_value_size(r, clcf->limit_rate, 0);
254 r->limit_rate_set = 1;
255 }
256
252257 if (r->limit_rate) {
253 if (r->limit_rate_after == 0) {
254 r->limit_rate_after = clcf->limit_rate_after;
258
259 if (!r->limit_rate_after_set) {
260 r->limit_rate_after = ngx_http_complex_value_size(r,
261 clcf->limit_rate_after, 0);
262 r->limit_rate_after_set = 1;
255263 }
256264
257265 limit = (off_t) r->limit_rate * (ngx_time() - r->start_sec + 1)