[nginx] Variables support in limit_rate and limit_rate_after (ticket #293).
Ruslan Ermilov
ru at nginx.com
Wed Apr 24 18:47:26 UTC 2019
details: https://hg.nginx.org/nginx/rev/c19ca381b2e6
branches:
changeset: 7504:c19ca381b2e6
user: Ruslan Ermilov <ru at nginx.com>
date: Wed Apr 24 16:38:54 2019 +0300
description:
Variables support in limit_rate and limit_rate_after (ticket #293).
diffstat:
src/http/ngx_http_core_module.c | 24 +++++++------
src/http/ngx_http_core_module.h | 5 +-
src/http/ngx_http_request.h | 3 +
src/http/ngx_http_upstream.c | 2 +
src/http/ngx_http_variables.c | 55 +++++++++++++++-----------------
src/http/ngx_http_write_filter_module.c | 12 ++++++-
6 files changed, 57 insertions(+), 44 deletions(-)
diffs (237 lines):
diff -r b82162b8496a -r c19ca381b2e6 src/http/ngx_http_core_module.c
--- a/src/http/ngx_http_core_module.c Wed Apr 24 16:38:51 2019 +0300
+++ b/src/http/ngx_http_core_module.c Wed Apr 24 16:38:54 2019 +0300
@@ -479,7 +479,7 @@ static ngx_command_t ngx_http_core_comm
{ ngx_string("limit_rate"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
|NGX_CONF_TAKE1,
- ngx_conf_set_size_slot,
+ ngx_http_set_complex_value_size_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_core_loc_conf_t, limit_rate),
NULL },
@@ -487,7 +487,7 @@ static ngx_command_t ngx_http_core_comm
{ ngx_string("limit_rate_after"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
|NGX_CONF_TAKE1,
- ngx_conf_set_size_slot,
+ ngx_http_set_complex_value_size_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_core_loc_conf_t, limit_rate_after),
NULL },
@@ -1281,10 +1281,6 @@ ngx_http_update_location_config(ngx_http
r->connection->tcp_nopush = NGX_TCP_NOPUSH_DISABLED;
}
- if (r->limit_rate == 0) {
- r->limit_rate = clcf->limit_rate;
- }
-
if (clcf->handler) {
r->content_handler = clcf->handler;
}
@@ -3387,6 +3383,8 @@ ngx_http_core_create_loc_conf(ngx_conf_t
* clcf->exact_match = 0;
* clcf->auto_redirect = 0;
* clcf->alias = 0;
+ * clcf->limit_rate = NULL;
+ * clcf->limit_rate_after = NULL;
* clcf->gzip_proxied = 0;
* clcf->keepalive_disable = 0;
*/
@@ -3417,8 +3415,6 @@ ngx_http_core_create_loc_conf(ngx_conf_t
clcf->send_timeout = NGX_CONF_UNSET_MSEC;
clcf->send_lowat = NGX_CONF_UNSET_SIZE;
clcf->postpone_output = NGX_CONF_UNSET_SIZE;
- clcf->limit_rate = NGX_CONF_UNSET_SIZE;
- clcf->limit_rate_after = NGX_CONF_UNSET_SIZE;
clcf->keepalive_timeout = NGX_CONF_UNSET_MSEC;
clcf->keepalive_header = NGX_CONF_UNSET;
clcf->keepalive_requests = NGX_CONF_UNSET_UINT;
@@ -3647,9 +3643,15 @@ ngx_http_core_merge_loc_conf(ngx_conf_t
ngx_conf_merge_size_value(conf->send_lowat, prev->send_lowat, 0);
ngx_conf_merge_size_value(conf->postpone_output, prev->postpone_output,
1460);
- ngx_conf_merge_size_value(conf->limit_rate, prev->limit_rate, 0);
- ngx_conf_merge_size_value(conf->limit_rate_after, prev->limit_rate_after,
- 0);
+
+ if (conf->limit_rate == NULL) {
+ conf->limit_rate = prev->limit_rate;
+ }
+
+ if (conf->limit_rate_after == NULL) {
+ conf->limit_rate_after = prev->limit_rate_after;
+ }
+
ngx_conf_merge_msec_value(conf->keepalive_timeout,
prev->keepalive_timeout, 75000);
ngx_conf_merge_sec_value(conf->keepalive_header,
diff -r b82162b8496a -r c19ca381b2e6 src/http/ngx_http_core_module.h
--- a/src/http/ngx_http_core_module.h Wed Apr 24 16:38:51 2019 +0300
+++ b/src/http/ngx_http_core_module.h Wed Apr 24 16:38:54 2019 +0300
@@ -349,13 +349,14 @@ struct ngx_http_core_loc_conf_s {
size_t client_body_buffer_size; /* client_body_buffer_size */
size_t send_lowat; /* send_lowat */
size_t postpone_output; /* postpone_output */
- size_t limit_rate; /* limit_rate */
- size_t limit_rate_after; /* limit_rate_after */
size_t sendfile_max_chunk; /* sendfile_max_chunk */
size_t read_ahead; /* read_ahead */
size_t subrequest_output_buffer_size;
/* subrequest_output_buffer_size */
+ ngx_http_complex_value_t *limit_rate; /* limit_rate */
+ ngx_http_complex_value_t *limit_rate_after; /* limit_rate_after */
+
ngx_msec_t client_body_timeout; /* client_body_timeout */
ngx_msec_t send_timeout; /* send_timeout */
ngx_msec_t keepalive_timeout; /* keepalive_timeout */
diff -r b82162b8496a -r c19ca381b2e6 src/http/ngx_http_request.h
--- a/src/http/ngx_http_request.h Wed Apr 24 16:38:51 2019 +0300
+++ b/src/http/ngx_http_request.h Wed Apr 24 16:38:54 2019 +0300
@@ -515,6 +515,9 @@ struct ngx_http_request_s {
unsigned limit_conn_set:1;
unsigned limit_req_set:1;
+ unsigned limit_rate_set:1;
+ unsigned limit_rate_after_set:1;
+
#if 0
unsigned cacheable:1;
#endif
diff -r b82162b8496a -r c19ca381b2e6 src/http/ngx_http_upstream.c
--- a/src/http/ngx_http_upstream.c Wed Apr 24 16:38:51 2019 +0300
+++ b/src/http/ngx_http_upstream.c Wed Apr 24 16:38:54 2019 +0300
@@ -2979,6 +2979,7 @@ ngx_http_upstream_send_response(ngx_http
ngx_http_upstream_process_non_buffered_downstream;
r->limit_rate = 0;
+ r->limit_rate_set = 1;
if (u->input_filter_init(u->input_filter_ctx) == NGX_ERROR) {
ngx_http_upstream_finalize_request(r, u, NGX_ERROR);
@@ -4806,6 +4807,7 @@ ngx_http_upstream_process_limit_rate(ngx
if (n != NGX_ERROR) {
r->limit_rate = (size_t) n;
+ r->limit_rate_set = 1;
}
return NGX_OK;
diff -r b82162b8496a -r c19ca381b2e6 src/http/ngx_http_variables.c
--- a/src/http/ngx_http_variables.c Wed Apr 24 16:38:51 2019 +0300
+++ b/src/http/ngx_http_variables.c Wed Apr 24 16:38:54 2019 +0300
@@ -22,8 +22,6 @@ static void ngx_http_variable_request_se
#endif
static ngx_int_t ngx_http_variable_request_get_size(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data);
-static void ngx_http_variable_request_set_size(ngx_http_request_t *r,
- ngx_http_variable_value_t *v, uintptr_t data);
static ngx_int_t ngx_http_variable_header(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data);
@@ -124,6 +122,8 @@ static ngx_int_t ngx_http_variable_sent_
ngx_http_variable_value_t *v, uintptr_t data);
static ngx_int_t ngx_http_variable_sent_transfer_encoding(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data);
+static void ngx_http_variable_set_limit_rate(ngx_http_request_t *r,
+ ngx_http_variable_value_t *v, uintptr_t data);
static ngx_int_t ngx_http_variable_connection(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data);
@@ -321,7 +321,7 @@ static ngx_http_variable_t ngx_http_cor
{ ngx_string("sent_http_link"), NULL, ngx_http_variable_headers,
offsetof(ngx_http_request_t, headers_out.link), 0, 0 },
- { ngx_string("limit_rate"), ngx_http_variable_request_set_size,
+ { ngx_string("limit_rate"), ngx_http_variable_set_limit_rate,
ngx_http_variable_request_get_size,
offsetof(ngx_http_request_t, limit_rate),
NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE, 0 },
@@ -788,32 +788,6 @@ ngx_http_variable_request_get_size(ngx_h
}
-static void
-ngx_http_variable_request_set_size(ngx_http_request_t *r,
- ngx_http_variable_value_t *v, uintptr_t data)
-{
- ssize_t s, *sp;
- ngx_str_t val;
-
- val.len = v->len;
- val.data = v->data;
-
- s = ngx_parse_size(&val);
-
- if (s == NGX_ERROR) {
- ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
- "invalid size \"%V\"", &val);
- return;
- }
-
- sp = (ssize_t *) ((char *) r + data);
-
- *sp = s;
-
- return;
-}
-
-
static ngx_int_t
ngx_http_variable_header(ngx_http_request_t *r, ngx_http_variable_value_t *v,
uintptr_t data)
@@ -1993,6 +1967,29 @@ ngx_http_variable_sent_transfer_encoding
}
+static void
+ngx_http_variable_set_limit_rate(ngx_http_request_t *r,
+ ngx_http_variable_value_t *v, uintptr_t data)
+{
+ ssize_t s;
+ ngx_str_t val;
+
+ val.len = v->len;
+ val.data = v->data;
+
+ s = ngx_parse_size(&val);
+
+ if (s == NGX_ERROR) {
+ ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
+ "invalid $limit_rate \"%V\"", &val);
+ return;
+ }
+
+ r->limit_rate = s;
+ r->limit_rate_set = 1;
+}
+
+
static ngx_int_t
ngx_http_variable_request_completion(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data)
diff -r b82162b8496a -r c19ca381b2e6 src/http/ngx_http_write_filter_module.c
--- a/src/http/ngx_http_write_filter_module.c Wed Apr 24 16:38:51 2019 +0300
+++ b/src/http/ngx_http_write_filter_module.c Wed Apr 24 16:38:54 2019 +0300
@@ -250,9 +250,17 @@ ngx_http_write_filter(ngx_http_request_t
return NGX_ERROR;
}
+ if (!r->limit_rate_set) {
+ r->limit_rate = ngx_http_complex_value_size(r, clcf->limit_rate, 0);
+ r->limit_rate_set = 1;
+ }
+
if (r->limit_rate) {
- if (r->limit_rate_after == 0) {
- r->limit_rate_after = clcf->limit_rate_after;
+
+ if (!r->limit_rate_after_set) {
+ r->limit_rate_after = ngx_http_complex_value_size(r,
+ clcf->limit_rate_after, 0);
+ r->limit_rate_after_set = 1;
}
limit = (off_t) r->limit_rate * (ngx_time() - r->start_sec + 1)
More information about the nginx-devel
mailing list